略微简单的动态规划
只是简单贴代码就好了。
#include <stdio.h> #include <string.h>int dp[100007][11]; int ans[100007][11]; int n,N;inline int Max(int x,int c){return x>c?x:c; } int v[16]; void DP() {int i,j;memset(v,0,sizeof(v));memset(ans,0,sizeof(ans));v[5]=v[4]=v[6]=1;int len = 2;for(i=1;i<=N+1;i++){for(j=0;j<=10;j++){if(v[j]){ans[i][j] = Max(dp[i][j]+ans[i-1][j],ans[i][j]); }if(j-1>=0 &&v[j-1]){ans[i][j-1] = Max(dp[i][j-1]+ans[i-1][j],ans[i][j-1]);}if(j+1<=10&&v[j+1]){ans[i][j+1] = Max(dp[i][j+1]+ans[i-1][j],ans[i][j+1]);}}if(len<=5){v[5-len]=1,v[5+len]=1;}len++;}int _max = -1100000000;for(int x=0;x<=10;x++) if(_max < ans[N+1][x]) _max = ans[N+1][x];printf("%d\n",_max); }void show() {for(int i=1;i<=N+1;i++){for(int x=0;x<=10;x++){printf("%d ",ans[i][x]);}puts("");} }int main() {int i;while(scanf("%d",&n),n){memset(dp,0,sizeof(dp));for(i=1;i<=n;i++){int ti,pot;scanf("%d%d",&pot,&ti);dp[ti][pot]++;if(ti > N) N = ti;}DP();//show(); }return 0; }