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

先把读入的区间从小到大排序,然后dp


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10005;
struct node
{
    int l,r,data;
    bool operator<(const node& tmp) const{
        if(l!=tmp.l)
            return l<tmp.l;
        else
            return r<tmp.r;
    }
}p[maxn];

int dp[maxn];//表示选择到第M个区间 能获得的最大值

int main()
{
    int n,m,r;
    while(~scanf("%d%d%d",&n,&m,&r)){
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].data);
            dp[i]=0;
        }
        sort(p,p+m);
        int j;
        for(int i=0;i<m;i++){
            int maxx=0;
            for(j=0;j<i;j++)if(p[i].l-p[j].r>=r){
                maxx=max(maxx,dp[j]);//找到这个区间前面的最大值
            }
            dp[i]=maxx+p[i].data;
        }
       printf("%d\n",*max_element(dp,dp+m+1));
    }
    return 0;
}
时间: 2024-10-14 02:32:06

R - Milking Time POJ 3616 ( DP )的相关文章

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 简单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 he

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

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

poj 3616 (DP)

题意: 给出m个区间 以及 每个区间的起点 .终点 .权值  m个区间会有重叠 要求从这些区间中选择若干个区间 ,使得这些所选区间的权值和最大 选择要求:1.所选区间无重叠部分  2.两个所选区间的间隔至少为r 思路: 线性结构上的DP 状态定义为: d[i] 代表 从前往后选择 选择到第i个区间时 选择第i个区间的最大权值 转移: d[i] = max(d[j]+w[i]); 其中d[j]应该满足 end[j] + r <= start[i]; code: const int maxn = 1

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

题目链接:http://poj.org/problem?id=3616 题目大意:M个区间,每个区间一个对应一个效率值-多少升牛奶,区间可能重复,现要求取出来一些区间,要求是区间间隔不能小于R,问所能得到的牛奶量的最大值. 解题思路:决策:当前区间用或者不用.区间个数M≤1000,因此直接双循环递推即可. dp[i]:=选第i个区间情况下前i个区间能获得的牛奶最大值 dp[i] = max(dp[i], dp[j] + a[i].eff) a[j].end + r <= a[i].start 代

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

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