POJ 3616 Milking Time(动态规划+题目描述坑人)

问题:Milking Time POJ - 3616

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_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 the N 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 the N hours


Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43

分析:

先将m个时间间隔按照开始时间进行排序

设dp[i]为从第i个时间间隔开始(包括第i个时间间隔)牛奶的最大产量

设sj为满足s>ei的s中最小的那一个

递推式子:

如果sj存在:dp[i]=max{dp[i+1],dp[j]+mi};

如果sj不存在:dp[i]=max{dp[i+1],mi};

mi是第i个时间间隔的产奶量,从右往左递推即可

注:这个题虽然说奶牛要休息r小时,但似乎就AC的情况来看,好像休息的第r小时就已经可以挤奶了。。。FJ和Bessie怕不是真爱。。。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct interval{
	int s,e,milk;
	bool operator < (const interval& c){
		return s<c.s;
	}
}inter[1010];
int dp[1010];//dp[i]代表从第i个区间往后走(包括第i个区间)的最大牛奶产量 
int main(){
	memset(inter,0,sizeof(inter));
	int n,m,r;
	cin>>n>>m>>r;//m是工作间隔的数目,n是总的时间,r是休息的时间
	n+=r;//修正总的时间 
	for(int i=0;i<m;i++){
		cin>>inter[i].s>>inter[i].e>>inter[i].milk;
		inter[i].e+=r;
	}
	sort(inter,inter+m);
	if(inter[m-1].e<=n){
		dp[m-1]=inter[m-1].milk;
	}
	else{
		dp[m-1]=0;
	} 
	for(int i=m-2;i>=0;i--){
		int E=inter[i].e;
		int tem=inter[i].milk;
		for(int j=i+1;j<m;j++){
			if(inter[j].s>=E){
				tem=dp[j]+inter[i].milk;break;
			}
		}
		dp[i]=max(dp[i+1],tem);
	}
	cout<<dp[0];	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333528/article/details/80143126