首页 > 其他 > 详细

POJ 3616 DP

时间:2015-05-07 12:28:45      阅读:154      评论:0      收藏:0      [点我收藏+]

奶牛在0~N时间段产奶。农夫约翰有M个时间段可以挤奶,时间段f,t内能挤到的牛奶量e。奶牛产奶后需要休息R小时才能继续下一次产奶,求最大的挤奶量


先对M个时间段进行排序,dp[i]表示前i个时间段最多可以产多少牛奶

if (data[j].r<=data[i].l)

dp[i]=Max(dp[i],dp[j]+data[i].x);


#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;

struct node
{
    int l,r,x;
}data[1010];

int dp[1010];

int Max(int a,int b)
{
    if (a<b) return b;
    else return a;
}

bool cmp(node a,node b)
{
    return a.l<b.l;
}
int main()
{
    int n,m,r,i,j,ans;
    while (scanf("%d%d%d",&n,&m,&r)!=EOF)
    {
        for (i=1;i<=m;i++)
        {
            scanf("%d%d%d",&data[i].l,&data[i].r,&data[i].x);
            data[i].r+=r;
        }
        sort(data+1,data+1+m,cmp);

        for (i=1;i<=m;i++)
            dp[i]=data[i].x;

        for (i=2;i<=m;i++)
            for (j=1;j<i;j++)
            {
                if (data[j].r<=data[i].l)
                    dp[i]=Max(dp[i],dp[j]+data[i].x);
            }
        ans=0;
        for (i=1;i<=m;i++)
            ans=Max(ans,dp[i]);
        printf("%d\n",ans);
    }
    return 0;
}


POJ 3616 DP

原文:http://blog.csdn.net/u011932355/article/details/45558183

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!