题目链接:免费馅饼
接馅饼,最开始在5,每秒只能移动一个单位,每次只能接住当前位置的馅饼
状态转移方程
dp[pos][t]=max(dp[pos][t-1],max(dp[pos-1][t-1],dp[pos+1][t-1]))+pie[pos][t];
dp[pos][t]表示在pos点,第t秒能接到的最多馅饼
几个注意:
1.pos取1-11,这样可以避免处理边界
2.注意状态转移的顺序
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxt=100005;
int dp[15][maxt];
int pie[15][maxt];
int main()
{
//freopen("in.txt","r",stdin);
int n,x,T,maxT,ans;
while(scanf("%d",&n)!=EOF&&n){
memset(dp,0,sizeof(dp));
memset(pie,0,sizeof(pie));
maxT=0;
ans=0;
while(n--){
scanf("%d%d",&x,&T);
pie[x+1][T]++;
maxT=max(maxT,T);
}
dp[5][1]=pie[5][1];
dp[6][1]=pie[6][1];
dp[7][1]=pie[7][1];
for(int t=2;t<=maxT;t++){
for(int pos=1;pos<=11;pos++){
dp[pos][t]=max(dp[pos][t-1],max(dp[pos-1][t-1],dp[pos+1][t-1]))+pie[pos][t];
}
}
for(int pos=1;pos<=11;pos++)ans=max(ans,dp[pos][maxT]);
printf("%d\n",ans);
}
return 0;
}