POJ 3616 Milking Time 線性dp

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: 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

題目大意

貝茜是一個勤勞的牛。事實上,她如此​​專注於最大化她的生產力,於是她決定安排下一個N(1≤N≤1,000,000)小時(方便地標記爲0…N-1),以便她生產儘可能多的牛奶。

農民約翰有一個M(1≤M≤1,000)可能重疊的間隔列表,他可以在那裏進行擠奶。每個區間我有一個起始小時(0≤starting_houri≤N),一個結束小時(starting_houri <ending_houri≤N),以及相應的效率(1≤efficiencyi≤1,000,000),表示他可以從中獲取多少加侖的牛奶。貝西在那段時間。 Farmer John分別在開始時間和結束時間開始時開始和停止擠奶。在擠奶時,Bessie必須在整個間隔內擠奶。

儘管貝茜有其侷限性。在任何間隔期間擠奶後,她必須休息R(1≤R≤N)小時才能再次開始擠奶。鑑於Farmer Johns的間隔清單,確定Bessie在N小時內可以產生的最大牛奶量。

Solution

線性dp,有點類似於LIS,利用時間的"序",先按每一時間段的開始時間從小到大排序(貪心思想),設dp[i] 爲以第i個時間段結尾的最大產量。
狀態轉移
dp[i]=max(dp[j] + val[i]) (node[i].start >= node[j].end + r)

代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

const int SZ = 2e3 + 20;

int ans,n,m,r,dp[SZ];

struct zt
{
	int start,end,val;
}node[SZ];

bool cmp(zt a,zt b)
{
	return a.start < b.start;
}

int main()
{
	scanf("%d%d%d",&n,&m,&r);
	for(int i = 1;i <= m;i ++ )
	{
		scanf("%d%d%d",&node[i].start,&node[i].end,&node[i].val);
	}
	sort(node + 1,node + m + 1,cmp);
	dp[0] = 0;
	node[0].start = node[0].end = -r;//邊界,任何一個都能從0轉移過來,即能做第一個
	node[0].val = 0;
	for(int i = 1;i <= m;i ++ )
	{
		dp[i] = node[i].val;
		for(int j = 0;j < i;j ++ )
		{
			if(node[i].start >= node[j].end + r)
			{
				dp[i] = max(dp[i],dp[j] + node[i].val);
			}
		}
		ans = max(ans,dp[i]);
	}
	printf("%d",ans);
	return 0;
}

2020.3.13

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章