HDU_1176_免费馅饼

http://acm.hdu.edu.cn/showproblem.php?pid=1176

 

参考自:http://blog.csdn.net/xcszbdnl/article/details/7876283

 

可将所有的时间段和馅饼看成是一个矩阵,时间就是行数,掉馅饼的就是列数,则就是数字三角形问题,从最底层找一条路径,使得路径上的和最大。

状态转移方程为:dp[i][j]=max(dp[i+1][j-1],dp[i+1][j],dp[i+1][j-1])+pie[i][j]。pie[i][j]为时间i时在j位置掉的馅饼数目。

则AC代码为:

#include<stdio.h>  
#include<string.h.>  
#define MAX 100001  
int pie[MAX][12];  
int dp[MAX][12];  
int n;  
int main()  
{  
    int i,j,time,location,maxtime,mid,left,right;  
    while(scanf("%d",&n)!=EOF&&n){  
        memset(pie,0,sizeof(pie));  
        memset(dp,0,sizeof(dp));  
        maxtime=0;  
        for(i=0;i<n;i++){  
            scanf("%d%d",&location,&time);  
            pie[time][location+1]++;  
            if(time>maxtime)  
                maxtime=time;  
        }  
        for(i=1;i<=11;i++)  
            dp[maxtime][i]=pie[maxtime][i];  
        for(i=maxtime-1;i>=0;i--){  
            for(j=1;j<=11;j++){  
                left=dp[i+1][j-1]+pie[i][j];  
                mid=dp[i+1][j]+pie[i][j];  
                right=dp[i+1][j+1]+pie[i][j];  
                dp[i][j]=(left>mid)?left:mid;  
                dp[i][j]=(dp[i][j]>right)?dp[i][j]:right;  
            }  
        }  
        printf("%d\n",dp[0][6]);  
    }  
    return 0;  
}  

 

posted on 2015-08-17 00:43  JASONlee3  阅读(162)  评论(0编辑  收藏  举报

导航