poj-3616milking time(dp)

题目链接:http://poj.org/problem?id=3616

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次机会挤奶,分别str,end,挤奶的多少value

找出能挤最多的奶

仍是dp,先将时间拍好,状态转移方程:dp[i]=max(dp[i],dp[j]+v[i])

从后往前找,对于每一个时间点,它本身所包含的价值和后面的能加上它的价值取最大的那个

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define da    0x3f3f3f3f  
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

struct ac{
	int st,ed,v;
}shuzu[1100];
int dp[1100];

int cmp(ac a,ac b)
{
	if(a.st==b.st)
		return a.ed<b.ed;
	return a.st<b.st;
}

int main()
{
	int n,m,r;
	cin>>n>>m>>r;
	for(int i=0;i<m;++i)
	{
		cin>>shuzu[i].st>>shuzu[i].ed>>shuzu[i].v;
		shuzu[i].ed=shuzu[i].ed+r;
	}
	sort(shuzu,shuzu+m,cmp);
	for(int i=m-1;i>=0;--i)//第一个只取自己 
	{
		dp[i]=shuzu[i].v;
		for(int j=i;j<m;++j)
		{
			if(shuzu[j].st>=shuzu[i].ed)
			{
				dp[i]=max(dp[i],dp[j]+shuzu[i].v);
			}
		}
	}
	int res=-1;
	for(int i=0;i<m;++i)
		res=res>dp[i]?res:dp[i];
	cout<<res<<endl;
	
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81076262