这道题也是想了好久,想了好久才想到数塔上来,想到之后心里那个happy啊,悲催的是,又开始纠结到底该怎么循环。因为是从中间点开始出发的,,,我去,顺序循环逆序循环都不行,,,最后还是看了看别人的代码才明白怎么处理这种情况,,,,,,,,,,学习了。。。。。。。。。。。题目:
6 5 1 4 1 6 1 7 2 7 2 8 3 0
4
#include <iostream> #include <cstdio> #include <string.h> using namespace std; const int N=100010; int dp[N][11],num[N][11],maxt,maxnum; int max2(int a,int b,int c){ int x=a>b?a:b; return x>c?x:c; } int max1(int a,int b){ return a>b?a:b; } void fun(){ dp[0][5]=num[0][5]; for(int t=1;t<=maxt;++t){ int begin=0,end=0; if(t>=5){ begin=0;end=10; } else{ begin=5-t;end=5+t; } for(int pos=begin;pos<=end;++pos){ if(pos==0){ dp[t][pos]=max1(dp[t-1][pos],dp[t-1][pos+1])+num[t][pos]; } else if(pos==10){ dp[t][pos]=max1(dp[t-1][pos],dp[t-1][pos-1])+num[t][pos]; } else{ dp[t][pos]=max2(dp[t-1][pos-1],dp[t-1][pos],dp[t-1][pos+1])+num[t][pos]; } if(dp[t][pos]>maxnum) maxnum=dp[t][pos]; } } } int main(){ //freopen("4.txt","r",stdin); int n; while(scanf("%d",&n)&&n){ memset(num,0,sizeof(num)); int a,b; maxt=0;maxnum=0; for(int i=1;i<=n;++i){ scanf("%d%d",&a,&b); num[b][a]++; if(b>maxt) maxt=b; } memset(dp,0,sizeof(dp)); fun(); printf("%d\n",maxnum); } return 0; }