Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10898   Accepted: 4591

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

 
 
 
-----------------------------------------------------------------------------------
 
 
分析:这题看到题首先想到的是O(nm)做法,即用dp[i][j]表示i到j的最大产奶数。然而这种做法针对这道题目,连数组都开不下,更别提时间了。
  正确做法是这样子的,因为m比较小,所以先按照时间进行排序,然后可以用dp[i]表示前i次产奶的最大值,
  递推关系:第i个时间段挤奶的最大值 = 前 i – 1 个时间段挤奶最大值中的最大值 + 第i次产奶量。
 
 
 #include <cstdio>
#include <algorithm>
using namespace std;
int dp[];
struct c{
int begin,end,e;
}a[];
bool cmp(c a,c b)
{
if(a.begin<b.begin) return true;
if(a.begin==b.begin&&a.end<b.end) return true;
return false;
}
int main()
{
int n,m,r;
scanf("%d%d%d",&n,&m,&r);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a[i].begin,&a[i].end,&a[i].e);
a[i].end+=r;// 实际的结束时间还需要加上休息时间
}
sort(a+,a++m,cmp);
for(int i=;i<=m;i++)
{
dp[i]=a[i].e;
for(int j=;j<=i;j++)
{
if(a[j].end<=a[i].begin)
dp[i]=max(dp[i],dp[j]+a[i].e);
}
}
printf("%d",*max_element(dp,dp++m));
return ;
}

【POJ】3616 Milking Time(dp)的更多相关文章

  1. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

  2. 【BZOJ】1068: [SCOI2007]压缩(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...

  3. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...

  4. 【POJ】2234 Matches Game(博弈论)

    http://poj.org/problem?id=2234 博弈论真是博大精深orz 首先我们仔细分析很容易分析出来,当只有一堆的时候,先手必胜:两堆并且相同的时候,先手必败,反之必胜. 根据博弈论 ...

  5. 【51nod1519】拆方块[Codeforces](dp)

    题目传送门:1519 拆方块 首先,我们可以发现,如果第i堆方块被消除,只有三种情况: 1.第i-1堆方块全部被消除: 2.第i+1堆方块全部被消除:(因为两侧的方块能够保护这一堆方块在两侧不暴露) ...

  6. 【bzoj1925】地精部落[SDOI2010](dp)

    题目传送门:1925: [Sdoi2010]地精部落 这道题,,,首先可以一眼看出他是要我们求由1~n的排列组成,并且抖来抖去的序列的方案数.然后再看一眼数据范围,,,似乎是O(n^2)的dp?然后各 ...

  7. 【ZOJ2278】Fight for Food(dp)

    BUPT2017 wintertraining(16) #4 F ZOJ - 2278 题意 给定一个10*10以内的地图,和p(P<=30000)只老鼠,给定其出现位置和时间T(T<=1 ...

  8. 【vijos】1764 Dual Matrices(dp)

    https://vijos.org/p/1764 自从心态好了很多后,做题的确很轻松. 这种题直接考虑我当前拿了一个,剩余空间最大能拿多少即可. 显然我们枚举每一个点拿出一个矩形(这个点作为右下角), ...

  9. 【Luogu】P3856公共子串(DP)

    题目链接 DP.设last[i][j]是第i个串字符'j'所在的最后的位置,f[i][j][k]是第一个串匹配到i,第二个串匹配到j,第三个串匹配到k,最多的公共子串数. 那么我们三重循环i.j.k, ...

随机推荐

  1. jQuery选项卡wdScrollTab

    实例Demo 运行一下 参数说明 Config active Number   Active tab index. Base on 0. autoResizable Boolean   Whether ...

  2. java事务(一)

    Java中事务处理的基本方法与原理,包含以下文章: (一)Java事务处理的基本问题 (二)失败的案例 (三)丑陋的案例 (四)成功的案例(自己实现一个线程安全的TransactionManager) ...

  3. ansible资料

    ansible系列教程-强烈推荐看完 ansible官方编写的例子 ansible_ui Jenkins配置ansible galaxy 官方文档 中文教程1 中文教程2 playbook进阶 YAM ...

  4. $timeout

    $timeout 会在执行后刷新页面上 与angular 相关的变量,在于jQuery共用修改页面变量时,这很可能会导致刷新跳动的现象:

  5. js之10天内免登陆

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. nginx初步尝试

    导师要我学习下nginx,弄个简单的负载均衡出来,具体就是请求发送到nginx上,然后nginx将请求转发到后面的两个jetty应用上,这两个应用的代码是一样的,只是监听的端口不同,由于是简单尝试,因 ...

  7. RankNet

    RankNet 论文的笔记:Learning to rank using gradient descent. 模型 特征 \(\mathbf x_i \in \mathbb R^d\) 模型函数:\( ...

  8. hiho1602本质不同的回文子串的数量

    给定一个字符串S,请统计S的所有子串中,有多少个本质不同的回文字符串? 注意如果两个位置不同的子串满足长度相同且对应字符也都相同,则认为这两个子串本质上是相同的. Input 一个只包含小写字母的字符 ...

  9. 51nod 1244 莫比乌斯函数之和 【杜教筛】

    51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...

  10. RUAL1519 Formula 1 【插头DP】

    RUAL1519 Formula 1 Background Regardless of the fact, that Vologda could not get rights to hold the ...