poj 3616 Milking Time dp

Milking Time

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: NM, 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

Source

题意:N为时间,M为奶牛的个数,R为每次挤奶的休息时间;
   后面M行,开始时间,结束时间,奶牛收益,求收益最大;
思路:dp[i]表示在i内的最大收益;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+10,M=1e6+10,inf=1e9;
struct is
{
    int l,r,p;
}a[N];
int cmp(is x,is y)
{
    if(x.r!=y.r)
    return x.r<y.r;
    return x.l<y.l;
}
int dp[M];
int main()
{
    int x,y,z,i,t;
    while(~scanf("%d%d%d",&x,&y,&z))
    {
        memset(dp,0,sizeof(dp));
        for(i=0;i<y;i++)
        scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].p);
        sort(a,a+y,cmp);
        for(i=0;i<y;i++)
        {
            dp[a[i].r]=max(dp[max(0,a[i].l-z)]+a[i].p,max(dp[a[i].r],dp[a[i].r-1]));
            if(i!=y-1)
            {
                int num=a[i].r+1;
                while(num<a[i+1].r)
                dp[num]=dp[num-1],num++;
            }
        }
        printf("%d
",dp[a[y-1].r]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5604960.html