1. 程式人生 > >Milking Time (poj 3616 簡單DP)

Milking Time (poj 3616 簡單DP)

Language: Milking Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5290 Accepted: 2183

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


題意:給個時間長度n,m個工作時間段和每個時間段能完成的工作量,一次只能做一個工作並且一旦開始做就要把它做完,要求選擇的兩個工作時間段之間至少相差r時間(中間需要休息嘛)求選擇那些工作n時間內能完成的最大工作量。輸出最大值。

思路:先按工作的結束時間從小到大排序,再動態規劃。dp[i]表示從頭開始取到第i段所獲得的最大值。二重迴圈,如果第i段之前的某個段的結束時間加上r小於等於第i段的開始時間,則更新dp[i]。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct St
{
    int s,t,w;
}st[maxn];

int dp[maxn];
int n,m,r;

int cmp(St a,St b)
{
    return a.t<b.t;
}

int main()
{
    int i,j;
    while (~sfff(n,m,r))
    {
        FRL(i,0,m)
            sfff(st[i].s,st[i].t,st[i].w);
        sort(st,st+m,cmp);
        int ans=-1;
        FRL(i,0,m)
        {
            dp[i]=st[i].w;
            FRL(j,0,i)
            {
                if (st[j].t+r<=st[i].s)
                    dp[i]=max(dp[i],dp[j]+st[i].w);
            }
            ans=max(ans,dp[i]);
        }
        pf("%d\n",ans);
    }
    return 0;
}


相關推薦

Milking Time (poj 3616 簡單DP)

Language: Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5290 Accepted: 2183 Description Bessie is such a

Dp Milking Time POJ - 3616

mat bsp urn 時間 clas ios ostream src ++ 題目大意: 一頭奶牛產奶的時間是1-n,農夫有m個時間段可以去收集奶,每次收了奶之後奶牛要休息R時間,求農夫可以收的奶的最大值。 每次自己要想蠻久都想不出怎麽去推,還是做的題太少啦。。。一看題解

Milking Time POJ

傳送門 題解:定義dp[i]表示第i個時間段擠奶能夠得到的最大值,拆開來說,就是前面 i – 1個時間段任取0到i – 1個時間段擠奶,然後加上這個時間段(i)的產奶量之和。dp[i]滿足如下遞推關係:第i個時間段擠奶的最大值 = 前 i – 1 個時間段擠奶最大值中的最大

POJ 1015 簡單dp 對映 路徑

要求:n個人,每個人有兩個指標p和d。選m個人,這些人的p之和為sump,d之和為sumd。求最小的abs(sumd-sump),若最小的abs(sumd-sump)不唯一,則最大的sumd+sump為滿足題意解,輸出sump和sumd,升序輸出這m個人序號。 方法:dp 1.dp[i][j

POJ 3186 簡單dp 類田忌賽馬

要求:一個盒子內有順序排列n個正整數,這n個正整數可以從盒子的左口出去也可以從右口出去,靠近埠的出去後遠離埠的才可以出去。一天出去一個正整數,若第k天出去的數為x,則這一天獲得價值為k*x。求n天可獲得的最大價值。 方法:簡單dp 類田忌賽馬 1.dp[i][j]表示前i天從左口出去了j個正

POJ 3666 簡單dp 離散化

要求:一個由正整數ai組成的長度為N數列,變成一個不增或不減數列所需的最小代價,一個數字變化1對應的代價為1。 資料範圍:1 ≤ N ≤ 2,000,0 ≤ ai ≤ 1,000,000,000 方法:簡單dp 離散化 1.ai太大了,因此需要離散化,假設求的是不減數列,將A陣列從小到大

POJ 1661 簡單dp

要求:老鼠在0時刻從位置(x,y)下落,x是橫座標,y是縱座標(即高度),下落速度是1m/s,有若干個與地面平行的平臺懸在空中,平臺的左右端點和高度確定,不存在重疊的平臺。老鼠若落在平臺的端點上,視作落在平臺上。老鼠下落高度不能超過maxh,求老鼠落地的最短時間。 方法:dp題 注意確定好狀態以

POJ 2533 簡單dp 最長上升子序列

要求:輸出一段序列a的最長上升子序列的長度。 方法:dp 1.這題有個二分的方法,但是我用暴力做的。 2.dp[i]表示以原序列中下標為i的元素結尾的子序列的最大長度。    dp[i] = max(dp[i] , dp[j] + 1) (j < i 且 a

POJ 1458 簡單dp 最長公共子序列

要求:輸出最長公共子序列的長度 方法:dp裸題 1.狀態:dp[i][j]表示第一個序列的前i個字元和第二個序列的前j個字元的最長公共子序列的長度。 2.狀態轉移方程在程式碼中。 3.首要是程式碼規範化,然後才是找bug。 #include<stdio.h> #inc

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(dp,類似於最長上升子序列)

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

POJ 3616 Milking TimeDP,區間和最大)

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

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

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 2229 Sumsets(簡單DP

nbsp 自己 arc arm 相加 res sum name base Farmer John commanded his cows to search for different sets of numbers that sum to a given number.