LeeBlog

导航

HDU 1176 免费馅饼

这题是一最优子结构的题,我开始暴力,结果一直TLE,最后听小白说这是一最优子问题,极像数塔,怎么像呢。这每一秒不就像极了每一层么,而且是从上往下时间增大的,为什么从下往上呢?因为要只到第一秒选哪个最大,就必须知道第二秒的分布情况,而要知道第二秒选哪个,就必须知道第三秒的情况,同理,可以一直往下推。这样不久像极了一个数塔么,要先解决下面的才知道上面的,而没一层的每一个又跟哪个有关系呢?显然由题意有每个只跟下面的三个有关,而且第0个和最后一个要特殊出理,所以有ch[y][++x]
#include<stdio.h>
#include<string.h>
int n,ch[100024][15],ms;
int cal(  )
{
    int max = 0;
    for( int t = ms - 1; t  ; --t )
    {
        for( int p = 1; p <= 11; ++p )
        {
            int c ,m = 0;
            for( c = p - 1; c <= p + 1; ++c )
                if( ch[t+1][c] > m )
                    m = ch[t+1][c];
            ch[t][p] += m;
            max = max > ch[t][p] ? max : ch[t][p];
        }
    }
    return max;
}
int main( )
{
    while( scanf( "%d",&n ),n )
    {
        memset( ch,0,sizeof( ch ) );
        ms = 0;
        for( int i = 1,x,y; i <= n; ++i )
        {
            scanf( "%d%d",&x,&y ),ch[y][++x]++;
            ms = ms > y ? ms : y;
        }
        printf( "%d\n",cal(  ) );
    }
    return 0;
}

posted on 2011-04-20 19:12  LeeBlog  阅读(198)  评论(0编辑  收藏  举报