HDU 1176 免费馅饼

一道简单的动态规划题,与http://acm.hdu.edu.cn/showproblem.php?pid=2084数塔思想是一样的,都是从后面往前面推;

View Code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[100024][12];
int sum[100024][12];
inline int Max( int a , int b )
{
return a > b ?a : b;
}
int main( )
{
int n,place, T;
while( scanf( "%d" ,&n ),n )
{
int maxT = 0 ;
memset( sum , 0 ,sizeof( sum ) );
memset( map , 0 ,sizeof( map ) );
for( int i = 0 ; i < n ; i++ )
{
scanf( "%d%d",&place,&T );
map[T][place]++;
if( maxT < T ) maxT = T;
}
for( int i = maxT ; i >=1 ; i-- )
{
for( int j = 0 ; j<=10 ; j++ )
{
int max ,maxsum;
if( j != 0 )
{
// max = Max( Max( map[i][j] , map[i][j-1] ),map[i][j+1] );
sum[i][j] = Max( Max( sum[i+1][j],sum[i+1][j+1] ) ,sum[i+1][j-1] )+map[i][j];
}
else
{
// max = Max( map[i][j] ,map[i][j+1] );
sum[i][j] = Max( sum[i+1][j],sum[i+1][j+1] )+map[i][j];
}
}
}
printf( "%d\n",Max(Max(sum[1][5],sum[1][4]),sum[1][6] ));
}
return 0;
}

 

你可能感兴趣的:(HDU)