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

题意:

每一个挤奶区间都有一个效率,奶牛每挤完依次奶需要休息R小时,问怎样安排才能使效率最高。

分析:

如果没有效率问题,那么按照结束时间递增排序总是最优的,这样可以覆盖到尽可能多的区间,当加上效率时,我们要考虑到前面的时间获得的效率是否比当前时间段的效率要更高。若以dp[k]表示第k个区间的效率(注意已经排好序,因而就是第k个结束时间的效率。则状态转移方程为:

dp[k] = max(dp[k], dp[j]+itv[j].ef);

代码:

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

const int MAX_N = 1000010;
const int MAX_M = 1010;

struct ITV {
    int st, ed, ef;
    bool operator < (const ITV& b) const {
        if(ed == b.ed) return st < b.st;
        return ed < b.ed;
    }
}itv[MAX_M];
int N, M, R, dp[MAX_M];

void solve() {
    sort(itv, itv+M);
    for(int i = 0; i < M; i++) dp[i] = itv[i].ef;
    int res = 0;
    for(int i = 0; i < M; i++)
        for(int j = 0; j < i; j++)
            if(itv[i].st - itv[j].ed >= R) {
                dp[i] = max(dp[i], dp[j]+itv[i].ef);
                if(dp[i] > res) res = dp[i];
            }
   printf("%d\n", res);
}

int main() {
    while(~scanf("%d%d%d", &N, &M, &R)) {
        for(int i = 0; i < M; i++)
            scanf("%d%d%d", &itv[i].st, &itv[i].ed, &itv[i].ef);
        solve();
    }
    return 0;
}
时间: 2024-10-10 16:39:49

POJ 3616 Milking Time 挤奶问题,带权区间DP的相关文章

[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

POJ 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int m

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 2492 A Bug&#39;s Life (带权并查集 &amp;&amp; 向量偏移)

题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配关系. 分析 : 本题雷同POJ 1182 食物链, 如果会了那一题, 那现在这题便简单多了, 建议先了解食物链的偏移向量做法.这里也是使用向量的思考方式来进行relation的变化, 这里我令 relation = 0为同性, relation = 1为异性, 接下来的步骤就和食物链的雷同了. 优

动态规划-带权区间问题

一.动态规划算法的定义: 为了着手开发一个动态规划算法,我们需要一组从初始问题导出的满足某些基本性质的子问题. 只存在多项式个子问题 可以容易的从子问题的解计算出初始问题的解 在子问题中,从"最小"到"最大"存在一种自然的顺序,与一个容易计算的递推公式相联系.这个递推公式允许我们从某些更小的子问题的解来确定一个子问题的解. 二.带权区间调度问题: 我们有N个需求,标记为1,2,3,......,N,每个需求指定一个开始时间si,结束时间fi ,每个需求i有一个权值v

POJ 3616 Milking Time(加掩饰的LIS)

传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13406   Accepted: 5655 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she dec

POJ 1182 食物链 [并查集 带权并查集 开拓思路]

传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1182 Appoint description:  System Crawler  (2015-01-27) Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物

【POJ 1984】Navigation Nightmare(带权并查集)

Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 100

POJ 1984 Navigation Nightmare 【经典带权并查集】

任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7783   Accepted: 2801 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usuall