DP-hdu1176

时间:2022-07-10 22:13:37

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176

这道题与动态规划中的数塔问题十分类似,因此如果对于数塔问题还不太明白的,可以先参考一下博客:

数塔问题:https://blog.csdn.net/theonegis/article/details/45801201

题目描述:

DP-hdu1176DP-hdu1176

代码实现:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int dp[maxn][];
///dp[i][j]表示:i秒时,在位置j上接到的饼数
int main()
{
int n,x,t,maxt;
while(~scanf("%d",&n),n){
maxt=;
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
scanf("%d%d",&x,&t);
dp[t][x+]++;///t时间在x位置掉一块馅饼则加1
if(t>maxt)///找出最后接饼的时间
maxt=t;
}
for(int i=maxt;i>=;i--){///从最后掉饼的时间开始,从下往上推
for(int j=;j<=;j++){///因为有0-10共十一个位置,因此循环执行到j<=11
dp[i-1][j]+=max(max(dp[i][j-1],dp[i][j]),dp[i][j+1]);///类似于数塔问题
}
}
printf("%d\n",dp[][]);///输出0秒6位置的最大馅饼数
}
return ;
}

相关文章