POJ3616——Milking Time(动态规划)

来源:互联网 发布:mac能用ios手游模拟器 编辑:程序博客网 时间:2024/05/17 03:44

题目链接

       这道题感觉有点像任务调度,(由于智商感人)还是看别人的题解做的。题目要求获得更多的牛奶,但区间又不能重叠,所以状态转移函数dp[i]=max(dp[i],dp[j]+s[i].product),(j表示在i之前的且与第i个区间不重合的区间),dp[i]表示第i个区间能获得的最大产量。其中第i个并不是题目给出的第i个区间,而是按照结束时间排好序的第i个区间。看一下代码还是很好懂的,,,就是自己想不出来~~~~忧桑。。。




#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>using namespace std;typedef long long LL;LL dp[1005]= {0};struct node{    LL starttime;    LL endtime;    LL product;    node()    {        starttime=0;        endtime=0;        product=0;    }} s[1005];bool cmp(node a,node b){    if(a.endtime==b.endtime)return a.starttime<b.starttime;    return a.endtime<b.endtime;}int main(){    //freopen("in.in","r",stdin);    int N, M, R;    while(scanf("%d%d%d",&N,&M,&R)!=EOF)    {        memset(s,0,sizeof(s));        memset(dp,0,sizeof(dp));        for(int i=0;i<M;i++)        {            scanf("%lld%lld%lld",&s[i].starttime,&s[i].endtime,&s[i].product);            s[i].endtime+=R;        }        sort(s,s+M,cmp);        for(int i=0;i<M;i++)        {            dp[i]=s[i].product;            for(int j=0;j<i;j++)                if(s[j].endtime<=s[i].starttime)                    dp[i]=max(dp[i],dp[j]+s[i].product);        }        LL max_n=dp[0];        for(int i=1;i<M;i++)            if(dp[i]>max_n)max_n=dp[i];        printf("%lld\n",max_n);    }    return 0;}

0 0
原创粉丝点击