DP-hdu1176
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176
这道题与动态规划中的数塔问题十分类似,因此如果对于数塔问题还不太明白的,可以先参考一下博客:
数塔问题:https://blog.csdn.net/theonegis/article/details/45801201
题目描述:
代码实现:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=100005; 7 int dp[maxn][15]; 8 ///dp[i][j]表示:i秒时,在位置j上接到的饼数 9 int main() 10 { 11 int n,x,t,maxt; 12 while(~scanf("%d",&n),n){ 13 maxt=0; 14 memset(dp,0,sizeof(dp)); 15 for(int i=0;i<n;i++){ 16 scanf("%d%d",&x,&t); 17 dp[t][x+1]++;///t时间在x位置掉一块馅饼则加1 18 if(t>maxt)///找出最后接饼的时间 19 maxt=t; 20 } 21 for(int i=maxt;i>=1;i--){///从最后掉饼的时间开始,从下往上推 22 for(int j=1;j<=11;j++){///因为有0-10共十一个位置,因此循环执行到j<=11 23 dp[i-1][j]+=max(max(dp[i][j-1],dp[i][j]),dp[i][j+1]);///类似于数塔问题 24 } 25 } 26 printf("%d\n",dp[0][6]);///输出0秒6位置的最大馅饼数 27 } 28 return 0; 29 }