poj3136 Milking Time

来源:互联网 发布:c语言基础知识txt下载 编辑:程序博客网 时间:2024/06/06 08:36
Description
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.


Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.


Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.


Input
* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi


Output
* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours


Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output

43


#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#define INF 0x3f3f3f3fint dp[1000+10];struct abcd{    int start;    int end;    int eff;}s[1000+10];int cmp(const void *a,const void *b){    return (*(struct abcd *)a).start-(*(struct abcd *)b).start;}int main(){    int m,n,r;    int i,j,k,max;    while(scanf("%d%d%d",&n,&m,&r)==3)    {        for(i=0;i<m;i++)            scanf("%d%d%d",&s[i].start,&s[i].end,&s[i].eff);        qsort(s,m,sizeof(struct abcd),cmp);        //for(i=0;i<m;i++)        //    printf("%d %d %d\n",s[i].start,s[i].end,s[i].eff);        memset(dp,0,sizeof(dp));        if(s[0].end<=n)            dp[0]=s[0].eff;        for(i=1;i<m;i++)        {            if(s[i].end<=n)                max=s[i].eff;            for(j=0;j<i;j++)            {                if(s[i].start>=s[j].end+r&&s[i].end<=n)                {                    if(dp[j]+s[i].eff>max)                        max=dp[j]+s[i].eff;                }            }            dp[i]=max;        }        for(i=0,max=0;i<m;i++)        {            if(dp[i]>max)                max=dp[i];        }        printf("%d\n",max);    }    return 0;}


0 0