Milking Time (贪心+dp)

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 Nhours

Sample Input

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

Sample Output

43

题意:

贝西是一头非常勤劳的母牛。事实上,她是如此的专注于最大化生产力,她决定安排接下来的N (1≤N≤1000000)小时(方便标记为0 . . N - 1),这样她产生尽可能多的牛奶。


农民约翰M的列表(1≤≤1000)可能重叠的时间间隔,可挤奶。每个间隔我开始有一个小时(0≤starting_houri≤N),结束一小时(starting_houri < ending_houri≤N),和一个相应的效率(1≤efficiencyi≤1000000)这表明多少加仑的牛奶,他可以离开贝西的间隔。农夫约翰在开始和结束的时刻开始和停止挤奶。贝茜挤奶时,整个间歇时间都要挤奶。


不过,即使是贝茜也有她的局限性。挤奶后在任何时间间隔内,她必须休息R R(1≤≤N)小时前她又可以挤奶。根据农场主约翰列出的间歇时间,确定贝茜在N小时内能产奶的最大限量。


输入

*第1行:三个空格分隔的整数:N、M和R

*线2 . .M+1:第i+1行描述FJ的第i个挤奶间隔,使用三个空格分隔的整数:starting_houri、ending_houri和efficient . i


输出

*第一行:贝茜在N小时内能生产的最大牛奶加仑数


样例输入

12 4 2

1 2 8

101219

3 6 24

7 10 31

样例输出

43

思路:

  先把数据按照结束时间从小到大排序,sort一下,然后动态规划

 d[i]表示从开始到第i次挤奶时的最大产奶量

状态转移方程:

 if(s[i].end+r<=s[j].start)
         dp[j]=max(dp[j],dp[i]+s[j].value);

 代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,r;
int dp[1005];
struct node{
	int start;
	int end;
	int value; 
}s[1005];
bool cmp(node a,node b)
{
	return a.end<b.end;
}
 int main(){
	cin>>n>>m>>r;
	for(int i=1;i<=m;i++)
	  scanf("%d%d%d",&s[i].start,&s[i].end,&s[i].value);
	sort(s+1,s+1+m,cmp);
	for(int i=1;i<=m;i++)
	   dp[i]=s[i].value;
	int ans=0;
	for(int i=1;i<=m;i++)
	{
		for(int j=i+1;j<=m;j++)
		    if(s[i].end+r<=s[j].start)
		      dp[j]=max(dp[j],dp[i]+s[j].value);
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/daoshen1314/article/details/84995762