1. 程式人生 > >POJ-3616 Milking Time 鶸的DP解題報告

POJ-3616 Milking Time 鶸的DP解題報告

題目:

Milking Time
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12358Accepted: 5242

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: 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個時間段,每段時間都有開始時間,結束時間,產生價值三個屬性。John可以選擇一些時間段來產生價值,當然選擇的時間段時間不能有重複,並且進行完一個時間段的工作之後,他必須休息r時間。求從這m個時間段中產生價值的最大值。

思路:首先要將所有時間段按照開始時間進行從小到大的排序。

之後定義dp[i]為:假設第i個時間段進行產奶時時,產生價值的總和的最大值。

狀態轉移方程為: dp[i] = max(dp[i] , dp[i]+list[j].value) ;( j<i , list[j].end+r<=list[i].start)

也就是說如果當前時間時間段產奶,產生價值的最大值為當前時間段產生的價值加上之前時間段所能產生的最大值。

而結果自然是所有假設中的最大值。

程式碼:

#include<cstdio>  
#include<cstdlib>  
#include<cmath>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
#include<queue>  
#include<map>  
#include<stack> 
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeace

typedef long long ll;

using namespace std;

const int INF = 0x3f3f3f3f;

struct fj{
	int s,e;
	int v;
}lis[1010];

bool cmp(fj x , fj y){
	if(x.s == y.s){
		return x.e < y.e;
	}
	return x.s < y.s;
}

int dp[1010];

int main(){
#ifdef Reast1nPeace
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	int n,m,r;
	cin>>n>>m>>r;
	for(int i = 0 ; i<m ; i++){
		cin>> lis[i].s >> lis[i].e >> lis[i].v;
	}
	sort(lis , lis+m ,  cmp);
	
	int maxn = 0;
	for(int i = 0 ; i<m ; i++){
		dp[i] = lis[i].v;
		for(int j = 0 ; j<i ; j++){
			if(lis[j].e+r <= lis[i].s){
				dp[i] = max(dp[i] , dp[j]+lis[i].v);
			}
		}
		maxn = max(dp[i] , maxn);
	}
	cout<<maxn<<endl;
	return 0;
}

相關推薦

POJ-3616 Milking Time DP解題報告

題目:Milking TimeTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 12358Accepted: 5242DescriptionBessie is such a hard-working cow. I

POJ 3616 Milking Time 簡單DP

++ cti als mission i++ mit wid ble turn 題目鏈接:http://poj.org/problem?id=3616 題目大意:M個區間,每個區間一個對應一個效率值-多少升牛奶,區間可能重復,現要求取出來一些區間,要求是區間間隔不能小於R,

POJ 3616 Milking TimeDP,區間和最大)

Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9937 Accepted: 4124 Des

poj 3616 Milking Time(dp,類似於最長上升子序列)

題意:給奶牛擠奶,共m次可以擠,給出每次開始擠奶的時間st,結束擠奶的時間ed,還有擠奶的量ef, 每次擠完奶要休息r時間,問最大擠奶量. 題解:此題靈感來自於最長上升子序列的做法 #include <iostream> #include <cstring>

POJ 3616 Milking Time 擠奶問題,帶權區間DP

Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4837 Accepted: 2034 D

POJ 3616 Milking Time DP題解

典型的給出區間任務和效益值,然後求最大效益值的任務取法。 屬於一維DP了。 一維table記錄的資料含義:到當前任務的截止時間前的最大效益值是多少。 注意, 這表示當前任務一定要選擇,但是最終結果是不一定選擇最後一個任務,故此最後需要遍歷找到table陣列的最大值,當然計算

POJ 3616 Milking Time(加掩飾的LIS)

interval targe rest style pri scanf cep oss before 傳送門: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS Memory Limit

POJ 3616 Milking Time

題目描述: Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next

POJ 3616 Milking Time 動態規劃

題意:一個奶牛在0~N時間段內可被取奶,每次擠奶以後必須休息至少R分鐘才能下次繼續擠奶。有M次可以擠奶的時間段,每次取奶對應三個值:開始時間、結束時間、效率值,每次擠奶的過程不能中斷。求出最大效率值

POJ - 3616 Milking Time (動態規劃)

lis 時間 indicate fine sig n) muc class ive Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity

POJ-3280 Cheapest Palindrome DP DP解題報告

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag

poj - 1185 炮兵陣地 狀壓DP 解題報告

其他 無法 popu mon 多少 mod tdi 遞推關系 r+ 炮兵陣地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21553 Accepted: 8363

hdu 5375 - Gray code(dp) 解題報告

auto 當前 width ive data -i int code original Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth

BZOJ 1068 [SCOI 2007] 區間DP 解題報告

bzoj href blank lower ace 解題報告 區間 gin win ijDLB51捌W到臀39林http://www.facebolw.com/space/2102699/follower gI135D0味51此癱9延http://www.facebolw

BZOJ1068[SCOI2007]區間DP解題報告

tid eva sina sgd icm uid bdn tpm store 30i56m擠舜埔悠逗匾http://www.docin.com/sina_6355808376m2nyk3站送秩墜燒輪http://tushu.docin.com/wwc70891ni5lk拙酉

POJ 1915(BFS_D題)解題報告

題目 pan 位置 open con play def sta 鏈接 題目鏈接:http://poj.org/problem?id=1915 -------------------------------------------------------- 題意:Chess中

POJ 1958 Strange Towers of Hanoi 解題報告

tran span lin line 移動 意思 公式 ron mat Strange Towers of Hanoi 大體意思是要求\(n\)盤4的的hanoi tower問題。 總所周知,\(n\)盤3塔有遞推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i]

[poj 2480] Longge's problem 解題報告 (歐拉函數)

ios ons src names def ref 技術 esp ++ 題目鏈接:http://poj.org/problem?id=2480 題目大意: 題解: 我一直很欣賞數學題完美的復雜度 #include<cstring> #inc

洛谷2889 [USACO07NOV]擠奶的時間Milking TimeDP)(樹狀陣列)

題意 奶牛Bessie在0~N時間段產奶。農夫約翰有M個時間段可以擠奶,時間段f,t內Bessie能擠到的牛奶量e。奶牛產奶後需要休息R小時才能繼續下一次產奶,求Bessie最大的擠奶量。 如果在(si,ti)時刻擠奶,那麼休息完的時間是si+r,即下一次可以擠奶的最早時間是(si+r,..

POJ3616 Milking Timedp

Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her