【POJ 3616】 Milking Time (动态规划dp)

 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

题意: 在时间n内,奶牛有m个时间段可以产奶,后面m行为每个时间段起始时间,结束时间以及产奶量,但每次产奶都需要休息r小时才可以进行下次产奶,求时间n内最大产奶量。

先对数据按结束时间由小到大进行排序,再通过状态转移方程:

                                                             dp[i]=max(dp[i],dp[j]+a[i].x);

求出第i个时间段的最大产奶量。

#include<iostream>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

struct ac
{
	int s,e,x;    //开始时间、结束时间、产奶量
}a[1000005];
bool cmp(ac x,ac y)
{
	if(x.s==y.s)
		return x.e<y.e;
	return x.s<y.s;
}
int main()
{
	int n,m,r,i,j,s=0,dp[1005];
	cin>>n>>m>>r;
	for(i=0;i<m;i++)
		cin>>a[i].s>>a[i].e>>a[i].x;
	sort(a,a+m,cmp);
	for(i=0;i<m;i++)
		dp[i]=a[i].x;
	for(i=0;i<m;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[j].e+r<=a[i].s)
			{
				dp[i]=max(dp[i],dp[j]+a[i].x);
			}
			s=max(s,dp[i]);
		}
	}
	cout<<s<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xylon_/article/details/81085661