1. 程式人生 > >POJ 3616 Milking Time(DP,區間和最大)

POJ 3616 Milking Time(DP,區間和最大)

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

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

Source

題目大意:        farmer John 要給女主牛 擠奶了。擠奶時間 N (1 ≤ N ≤ 1,000,000) hours,注意Bessie進了一間黑心企業,每次擠奶休息完R (1 ≤ R ≤ N)hours後,又可以擠。然後farmer John 有M個時間段,開始時間s,結束時間e,一旦開始就不能停下,直到擠完奶。在這些時間段裡可以給Bessie擠奶,但每個時間段的效率不同,有個擠得多一點,有得少一點。現在你的任務是根據famer john的時間段,選擇搭配起來最有效率的時間段來讓Bessie產奶最多。 這就是一道區域的最大求和問題。 區域的最大求和     給定一個大的區間,裡面有相交的區域,每個區域都代表了一個值,求取得的區域和最大。 模版程式碼
#define MAXN //區間數 
struct node{
	int s;//區域開始的下標 
	int e;//區域結束的下標 
	int w;//區域代表的值 
}a[MAXN];
int dp[MAXN];
int main(){
	sort(a,a+m);//按開始的下標排序 
	int ans=0;
	for(int i=0;i<m;i++){
		dp[i]=a[i].w;
		for(int j=0;j<i;j++){
			if(a[i].s>=a[j].e)
			dp[i]=max(dp[i],dp[j]+a[i].w);
			ans=max(ans,dp[i]);
		}
	}
}
在這題中,只需要把每次擠奶結束的時間加上休息的時間就是最終結束的時間 AC程式碼
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
	int s,e,w;
}a[1005];
int dp[1005];
bool cmp(node a,node b){
	if(a.s<b.s)return true;
	if(a.s==b.s&&a.e<b.e)return true;
	return false;
}
int main(){
	int n,m,r;
	scanf("%d%d%d",&n,&m,&r);
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].w);
		a[i].e+=r;
	}
	sort(a,a+m,cmp);
	int ans=0;
	for(int i=0;i<m;i++){
		dp[i]=a[i].w;
		for(int j=0;j<i;j++){
			if(a[i].s>=a[j].e)
			dp[i]=max(dp[i],dp[j]+a[i].w);
			ans=max(ans,dp[i]);
		}
	}
	printf("%d\n",ans);
	return 0;
}



相關推薦

POJ 3616 Milking TimeDP區間

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

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

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

POJ 3616 Milking Time 簡單DP

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

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 2018二分區間平均數字首

題目連結:http://poj.org/problem?id=2018 題意,給定一個非負序列,求長度大於F的連續子序列的平均數最大 解法:在實數上二分平均數mid,判斷a中是否有長度大於F平均數大於等

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

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

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題解

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

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

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

POJ - 2392】Space Elevator dp優秀的揹包問題

題幹: The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K

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

線段樹總結單點更新區間更新區間求和區間

注:每個功能在程式碼中有註釋,具體詳解可自己輸出測試 #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 4000

codeforces 9D How many trees?DP注意狀態表示方法

題目連結 分析:比較一下各種狀態表示, ①dp[n][h] 若表示n個節點深度為h,需要列舉左右兒子的深度,則每次轉移需要O(n*h^2),不夠優;  ②若dp[n][h]表示n個節點深度大於等於h,轉移時的條件是至少有一個兒子的深度大於等於h-1,發現轉移略複雜,是:[“

POJ-1273-Drainage Ditches網絡流之

rom lang spa bsp pen from per int eof Every time it rains on Farmer John‘s fields, a pond forms over Bessie‘s favorite clover patch. This

HDU 5550 - Game RoomsDP + 前綴預處理

pro esp \n microsoft -s print ase game type 鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 題意: 一個大樓有n(2≤n≤4000)層,每層可以建一個乒乓球房或者一個遊泳房

Java面向物件概述及三大特徵封裝繼承多型

一、面向物件思想 Java是面向物件的高階語言,對於Java語言來說,萬事萬物皆物件! 它的基本思想是使用類,物件,繼承,封裝,訊息等基本概念進行程式設計。面向物件程式的最小單元是類,類代表了客觀世界中具有某一特徵的一類事物,封裝了這類事物所具有的屬性和行為。 所以,類定義=成員變數(屬性)+方法(行為

給定一個數組求其某個子區間區間

#include<iostream> #include<vector> #include<iterator> #include<type_traits> #include<ctime> #include<ran

點估計矩估計法似然估計法

估計即是近似地求某個引數的值,需要區別理解樣本、總體、量、值 大致的題型是已知某分佈(其實包含未知引數),從中取樣本並給出樣本值 我只是一個初學者,可能有的步驟比較繁瑣,請見諒~ 1、矩估計法