poj 3616 Milking Time DP

来源:互联网 发布:非农数据大于预期 编辑:程序博客网 时间:2024/05/16 00:34
Milking Time
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4129 Accepted: 1720

Description

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her nextN (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 intervali has a starting hour (0 ≤ starting_houriN), an ending hour (starting_houri <ending_houriN), 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 restR (1 ≤ RN) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in theN 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 theN hours

Sample Input

12 4 21 2 810 12 193 6 247 10 31

Sample Output

43

Source

USACO 2007 November Silver


Source CodeProblem: 3616Memory: 240KTime: 47MSLanguage: C++Result: Accepted    Source Code    #include <iostream>    #include <stdio.h>    #include <algorithm>    using namespace std;    typedef struct{    int start;    int end;    int value;    }Cow;    Cow cow[1024];    int dp[1024];    int cmp(const void *a, const void *b)    {    Cow *_a = (Cow *)a;    Cow *_b = (Cow *)b;    return _a->start - _b->start;    }    int Max_value(int a, int b)    {    return a>b?a:b;    }    int main()    {    //freopen("in.txt","r",stdin);    int N,M, R;    cin >> N >> M >> R;    for(int i=0; i < M; i++)    {    cin >> cow[i].start >>cow[i].end >> cow[i].value;    cow[i].end += R;    }    qsort(cow, M, sizeof(cow[0]), cmp);    for(int i=0; i < M; i++)    {    dp[i] = cow[i].value;    for(int j=0; j < i; j++)    {    if(cow[i].start >= cow[j].end)    dp[i]=Max_value(dp[i], dp[j]+cow[i].value);    }    }    cout << *max_element(dp,dp+M) << endl;    return 0;    }


0 0
原创粉丝点击