poj3616

来源:互联网 发布:rgb颜色渐变算法 c 编辑:程序博客网 时间:2024/05/29 09:31
Milking Time
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9018 Accepted: 3738

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 21 2 810 12 193 6 247 10 31

Sample Output

43

Source

USACO 2007 November Silver


刚拿到手还以为是要背包呢,结果那个N一直没用到就水过了了。
思路大概是  根据结束时间拍下序,然后dp找出最大的收益就行了,没啥绕的。
贴上代码
#include<iostream>#include<string.h>#include<algorithm>#include<cstdio>using namespace std;struct node{int st;int en;int co;};int dp[1050];bool cmp(node x,node y){     return x.en<y.en;}int main(){node ans[10000];int n,m,r;while(cin>>n>>m>>r){for(int i=0;i<m;i++){scanf("%d%d%d",&ans[i].st,&ans[i].en,&ans[i].co);ans[i].en+=r;} //int stx,enx;memset(dp,0,sizeof(dp));sort(ans,ans+m,cmp);        for(int i=0;i<m;i++){        dp[i]=ans[i].co;        for(int j=0;j<i;j++){        if(ans[j].en<=ans[i].st){        dp[i]=max(dp[i],dp[j]+ans[i].co);//记录每个字段的和 等最后来找出最大就行了。}}}cout<<*max_element(dp, dp + m)<<endl;//找出最大值}}


0 0