题目:http://acm.hdu.edu.cn/showproblem.php?pid=1176
#include <iostream> using namespace std; int max1(int a,int b,int c) { if(a>b) return a>c?a:c; else return b>c?b:c; } int dp[100005][11]={0};//dp[i][j]代表第i秒时第j个位置能得到的最大值 int cook[100005][11]={0}; int main(int argc, const char *argv[]) { //freopen("input.txt","r",stdin); int n,x,T,max_T; while(~scanf("%d",&n)&&n) { memset(dp,0,sizeof(dp)); memset(cook,0,sizeof(cook)); max_T = 0; while(n--) { scanf("%d%d",&x,&T); if(T>max_T) { max_T = T; } cook[T][x]++; } for(int i=max_T;i>=0;i--) { for(int j=0;j<=10;j++) { if(j==0) dp[i][j]=max1(dp[i+1][j],dp[i+1][j+1],0); else if(j==10) dp[i][j]=max1(dp[i+1][j],dp[i+1][j-1],0); else dp[i][j]=max1(dp[i+1][j],dp[i+1][j-1],dp[i+1][j+1]); dp[i][j]+=cook[i][j]; } } cout<<dp[0][5]<<endl; } }