poj3616(dp)

来源:互联网 发布:游戏程序员待遇 编辑:程序博客网 时间:2024/06/05 19:26

Milking Time 

Time Limit: 1000MS

Memory Limit: 65536K

[显示标签]

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 ofM (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 rest R (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 , andefficiencyi

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


题意:给奶牛挤奶,现在一共有n长的时间,有m个时间段(可重叠)可以挤奶,每个时间段可以挤的奶也不一样多,每挤一次奶需要休息r时间,问在n长的时间内,最多能挤多少奶。

思路:以m个区间来dp,求出前i个时间段内最多的挤奶量,转移公式对于dp[i]是前i-1个时间段中与i时间段不重叠的最大值加上在时间段i产生的奶,从前到后把所有时间段前的最大挤奶量更新完,由于时间可以重叠,最后一个时间段前的挤奶量dp[m-1]不一定是最大的,要逐个比较。



#include<iostream>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;#define maxm 1005int n,m,r;int dp[maxm];struct Node{int start,end,eff;};Node interval[maxm];bool cmp(const Node a,const Node b){if(a.start==b.start)return a.end<b.end;return a.start<b.start;}int main(){while(cin>>n>>m>>r){for(int i=0;i<m;i++){cin>>interval[i].start>>interval[i].end>>interval[i].eff;//真实的结束时间还要加上休息时间,最后一次挤奶后不再挤奶,所以真实结束时间超过n也不要紧 interval[i].end+=r;}sort(interval,interval+m,cmp);   //按开始和结束顺序先后排序 for(int i=0;i<m;i++){dp[i]=interval[i].eff;for(int j=0;j<i;j++){if(interval[j].end<=interval[i].start){dp[i]=max(dp[i],dp[j]+interval[i].eff);}}}int res=0;for(int i=0;i<m;i++)if(dp[i]>res)res=dp[i];cout<<res<<endl;}}



原创粉丝点击