Milking Time (poj 3616 简单DP)


Language:
Default

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

Source

USACO 2007 November Silver

题意:给个时间长度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;
}
时间: 2024-11-05 21:58:39

Milking Time (poj 3616 简单DP)的相关文章

R - Milking Time POJ 3616 ( DP )

R - Milking Time Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3616 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedul

Dp Milking Time POJ - 3616

题目大意: 一头奶牛产奶的时间是1-n,农夫有m个时间段可以去收集奶,每次收了奶之后奶牛要休息R时间,求农夫可以收的奶的最大值. 每次自己要想蛮久都想不出怎么去推,还是做的题太少啦...一看题解 知道dp[i]表示区间[1,i]所能得到牛奶的最大值后,一下就写出来啦. 思路类似于求最长递增子序列. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #includ

Milking Time POJ - 3616 dp 感觉像背包

#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; const int N=1010; struct edge{ int start; int end; int w; }e[N]; bool cmp(edge a,edge b) { return a.start<b.start; } int dp[N]; int m

Milking Time POJ - 3616

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 ha

Wooden Sticks POJ 1065(简单dp)

原题 题目链接 题目分析 题意很明确,就是要维护单调递增的序列,最后看有多少种单调序列即可,设定一个dp数组,cnt表示数组大小,初始化为0,然后把所有木头从小到大排个序,当遍历到木头i时,如果dp数组里有比木头i还小的,就代替它,否则就dp[cnt++]=木头i,最后cnt就是答案. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio>

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <

poj 3616 Milking Time dp+树状数组

题意: 给一堆区间,每个区间有开始时间s,结束时间e,和收益w,现在要找一些区间使收益和最大,且区间之间的间隔最小为r. 分析: 这道题用dp做是简单题,用dp+树状数组做是中等题.dp问题的关键是对状态的定义.有两种方法,一:dp[k]表示按开始时间排序到第k个区间能取得的最大收益.二:dp[t]表示在时间t时能获得的最大收益.定义好状态方程就好写了这不再赘述.有趣的是这个时间复杂度.设一共有M个区间,所有区间的最大时间为L,第一种是M^2的,第二种是M*(logL+logM)的,这题M才10

POJ 3616 Milking Time 挤奶问题,带权区间DP

题目链接:POJ 3616 Milking Time Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4837   Accepted: 2034 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to sc

[2016-03-28][POJ][3616][Milking Time]

时间:2016-03-28 17:27:03 星期一 题目编号:[2016-03-28][POJ][3616][Milking Time] #include <algorithm> #include <cstdio> using namespace std; const int maxm = 1000 + 10; struct Roo{ int l,r,v; bool operator < (const Roo & a)const{ return l < a.l