免費餡餅 hdu 1176
阿新 • • 發佈:2018-12-23
其實已經是第二次做這個題了emmm
看完漫畫終於能夠基本理解動態規劃的意思
對於0,5這個點來說。他要用的就是通過下一個位置可能的最大收益來判斷
所以下層是dp[i+1]
陣列回去的時候是用i=tot往回
還有就是在0和10的時候要避免越界的情況出現。
#include<stdio.h>
#include<string.h>
int max(int a,int b,int c){
int ans=a;
if(b>ans) ans=b;
if(c>ans) ans=c;
return ans;
}
int p[100005 ][13];
int dp[100005][13];
int main()
{
int n,tot=0,x,t;
while(scanf("%d",&n) && n!=0)
{
memset(p,0,sizeof(p));
memset(dp,0,sizeof(dp));
tot=0;
while(n--)
{
scanf("%d%d",&x,&t);
if(t>tot) tot=t;
p[t][x]++;
}
//代表的是第i秒站在j位置上
for(int i=tot;i>=0;i--)
for(int j=0;j<=10;j++)
{
if(j==0) dp[i][j]=max(dp[i+1][j]+p[i+1][j],dp[i+1][j+1]+p[i+1][j+1],0);
else if(j==10) dp[i][j]=max(dp[i+1][j]+p[i+1][j],dp[i+1][j-1]+p[i+1][j-1],0);
else dp[i][j]=max(dp[i+1 ][j]+p[i+1][j],dp[i+1][j-1]+p[i+1][j-1],dp[i+1][j+1]+p[i+1][j+1]);
}
printf("%d\n",dp[0][5]);
}
return 0;
}