【HDU1176】免费馅饼

来源:互联网 发布:双色球开奖历史数据库 编辑:程序博客网 时间:2024/06/05 18:34

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176
题解:
dp[i][j]表示第i秒时在位置j时包里的馅饼;
如果把每一刻能否得到馅饼画出来,大致就是这个样子

0514 5 623 4 5 6 732 3 4 5 6 7 841 2 3 4 5 6 7 8 950 1 2 3 4 5 6 7 8 9 1060 1 2 3 4 5 6 7 8 9 107.................  

如果当前位置有馅饼,dp值就是1,否则是0
然后就是最简单的数塔了,是取三个位置的最大值
dp[i][j]=max{dp[i+1][j],dp[i+1][j-1],dp[i+1][j+1]}+value[i][j];
至于0这个位置,可以把坐标轴右移一位,就不需要特判了
还是很水的一道题

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,dp[200010][20];int main(){    int t,x,maxt,nmax;    while(scanf("%d",&n)&&n)    {        memset(dp,0,sizeof(dp));        maxt=0;        for(int i=1;i<=n;i++)        {            scanf("%d%d",&x,&t);            dp[t][x+1]++;            maxt=max(maxt,t);        }        for(int i=maxt-1;i>=0;i--)            for(int j=1;j<=11;j++)            {                nmax=max(dp[i+1][j-1],dp[i+1][j]);                nmax=max(dp[i+1][j+1],nmax);                dp[i][j]+=nmax;            }        printf("%d\n",dp[0][6]);    }    return 0;}