题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176
6 5 1 4 1 6 1 7 2 7 2 8 3 0
4
//AC 62ms 10432k(by C++) #include<stdio.h> #include<string.h> int pie[100005][13]; int dp[100005][13]; int maxn(int a,int b,int c) { a=a>b? a : b; a=a>c? a : c; return a; } int main() { int n,maxtime=0,ans=0; int x,t; int i,j; while(scanf("%d",&n)!=EOF && n) { memset(pie,0,sizeof(pie)); memset(dp,0,sizeof(dp)); while(n--) { scanf("%d%d",&x,&t); pie[t][x+1]++; //注意边界处理,由于有j-1,故:所有的馅饼下落位置往后移一位,否则会WA的很惨的 maxtime=maxtime>t ? maxtime : t; } for(i=maxtime,j=0;j<=12;j++) dp[i][j]=pie[i][j]; for(i=maxtime-1;i>=1;i--) //注意:类似于数塔问题,从下往上递推 for(j=1;j<=11;j++) //由于有j-1,注意边界处理 ,否则会WA的很惨啊 ,奇怪的是前面移位后,这里写(j=0;j<=11;j++)也过了 ,额,原来是自己看错了,移位后,馅饼也只落在1---11 dp[i][j]=pie[i][j]+maxn(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1]); ans=maxn(dp[1][5],dp[1][6],dp[1][7]); printf("%d\n",ans); } return 0; }