描述


http://poj.org/problem?id=3616

给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量.

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7507   Accepted: 3149

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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

  1. 12 4 2
  2. 1 2 8
  3. 10 12 19
  4. 3 6 24
  5. 7 10 31

Sample Output

  1. 43

Source

分析


对于每一次挤奶,结束时间+=休息时间.

先把m次挤奶按照开始时间排个序,用f[i]表示挤完第i个时间段的奶以后的最大挤奶量,那么有:

f[i]=max(f[i],f[j]+(第i次挤奶.ef)) (1<=j<i&&(第j次挤奶).ed<=(第i次挤奶).st).

注意:

1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因为不一定最后一次挤奶是哪一次).

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. const int maxm=;
  6. struct node
  7. {
  8. int st,ed,ef;
  9. bool operator < (const node &a) const
  10. {
  11. return a.st>st;
  12. }
  13. }a[maxm];
  14. int n,m,r;
  15. int f[maxm];
  16.  
  17. void solve()
  18. {
  19. for(int i=;i<=m;i++)
  20. {
  21. f[i]=a[i].ef;
  22. for(int j=;j<i;j++)
  23. {
  24. if(a[j].ed<=a[i].st)
  25. {
  26. f[i]=max(f[i],f[j]+a[i].ef);
  27. }
  28.  
  29. }
  30. }
  31. int ans=f[];
  32. for(int i=;i<=m;i++) ans=max(ans,f[i]);
  33. printf("%d\n",ans);
  34. }
  35.  
  36. void init()
  37. {
  38. scanf("%d%d%d",&n,&m,&r);
  39. for(int i=;i<=m;i++)
  40. {
  41. scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef);
  42. a[i].ed+=r;
  43. }
  44. sort(a+,a+m+);
  45. }
  46.  
  47. int main()
  48. {
  49. #ifndef ONLINE_JUDGE
  50. freopen("milk.in","r",stdin);
  51. freopen("milk.out","w",stdout);
  52. #endif
  53. init();
  54. solve();
  55. #ifndef ONLINE_JUDGE
  56. fclose(stdin);
  57. fclose(stdout);
  58. #endif
  59. return ;
  60. }

POJ_3616_Milking_Time_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. Java 十进制转十六进制

    1. /** * All possible chars for representing a number as a String */ final static char[] digits = { ...

  2. Angularjs2——TypeScript学习网站

    https://zhongsp.gitbooks.io/typescript-handbook/content/index.html

  3. OC 消息传递机制

    消息传递(message passing)的概念是Smalltalk语言的核心原则之一,有时Smalltalk和Objective-C被称为面向消息的语言,通常“消息”一词的含义和“方法”是相同的. ...

  4. 反射 介绍System.Type类

    本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...

  5. 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结

    请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...

  6. Excel对话框大全

    Excel对话框大全 序号 名称 描述 1 Application.Dialogs(1).Show 是调用打开对话框  2 Application.Dialogs(5或145).Show 是调用另存为 ...

  7. HDU 1300 Pearls (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 题目大意:珠宝店有100种不同质量的珍珠,质量越高价钱越高,为了促进销售,每买一种类型的珍珠,要 ...

  8. 网易邮箱前端Javascript编码规范:基础规范

    在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...

  9. C语言小结之链表

    链表的学习 在数据结构中有一种结构叫做线性表,线性表是储存一个线性数据的表格,本文就简要的介绍一下线性表的构成. 一.线性表的定义定义:由同种类型数据元素构成的有序数列的线性结构长度.表头.表尾Lis ...

  10. STM32内存映射

    一.概述 STM32内存映射是STM32的架构的重要组成部分,不可或缺. 二.STM32内存映射图 1.内存映射图--摘自<CM3权威指南> 2.内存映射图--摘自<STM32F10 ...