传送门:

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

 
题目意思:
奶牛为自己规划下面n时间内的产奶,m个时间段,每个段有a,b,c表示从a时到b时共可产奶c。
挤奶工每次挤奶必须挤完完整的时间段,且每次挤完需要休息r时,求最终可获得的牛奶最大值
 
做法:
按结束时间(=结束时间+时间休息)排个序 贪心的思想,为什么是按照结束时间排序,具体请参考贪心经典样例之活动安排问题
为什么:是为了剩余时间最大化
 
    先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
    然后用动态规划做。
    dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
    需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
    求出最大值加上第i个时间区间的产奶量就是dp[i],
    最后去dp数组最大值即可。
 
code:
 
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<iostream>
  4. using namespace std;
  5. #define max_v 1005
  6. struct node
  7. {
  8. int s,f,v;
  9. }p[max_v];
  10. bool cmp(node a,node b)
  11. {
  12. return a.f<b.f;
  13. }
  14. int dp[max_v];
  15. int main()
  16. {
  17. /*
  18. 先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
  19. 然后用动态规划做。
  20. dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
  21. 需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
  22. 求出最大值加上第i个时间区间的产奶量就是dp[i],
  23. 最后去dp数组最大值即可。
  24. */
  25. int n,m,r;
  26. while(cin>>n>>m>>r)
  27. {
  28. for(int i=;i<m;i++)
  29. {
  30. scanf("%d %d %d",&p[i].s,&p[i].f,&p[i].v);
  31. p[i].f+=r;
  32. }
  33. sort(p,p+m,cmp);
  34. for(int i=;i<max_v;i++)
  35. dp[i]=;
  36. dp[]=p[].v;
  37. int ans=dp[];
  38. for(int i=;i<m;i++)
  39. {
  40. int t=;
  41. for(int j=;j<i;j++)
  42. {
  43. if(p[j].f<=p[i].s)
  44. {
  45. t=max(t,dp[j]);
  46. }
  47. }
  48. dp[i]=t+p[i].v;
  49. ans=max(ans,dp[i]);
  50. }
  51. printf("%d\n",ans);
  52. }
  53. return ;
  54. }

POJ 3616 Milking Time(加掩饰的LIS)的更多相关文章

  1. POJ 3616 Milking Time (排序+dp)

    题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次 ...

  2. POJ 3616 Milking Time(最大递增子序列变形)

    题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产, ...

  3. poj 3616 Milking Time (基础dp)

    题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...

  4. poj 3616 Milking Time

                                                                                                 Milking ...

  5. poj 3616 Milking Time(dp)

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

  6. POJ - 3616 Milking Time (动态规划)

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  7. POJ 3616 Milking Time 简单DP

    题意:奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息R小时才能继续下一次产奶,求Bessie最大的挤奶量. 详见代码 ...

  8. POJ 3616 Milking Time (字符串DP)

    题意:找元素关于对角线左或右对称的最大矩阵 思路:左右对角线只需要遍历一条就可以了.只要当前点往上遍历和往后遍历一样就可以. #include<iostream> #include< ...

  9. poj 3616 Milking Time DP

    题意:在给予的N个时间里,奶牛Bessie在M个时间段里进行产奶,但是每次产奶后都要休息R个时间 M个时间段里,分别有开始时间start和结束时间end,和该时间段里产奶的效率efficiency 求 ...

随机推荐

  1. 二、hadoop文件操作

    1.使用hadoop命令查看hdfs下文件 [root@localhost hadoop-2.7.2]# hadoop fs -ls hdfs://192.168.211.129:9000/  (最后 ...

  2. .net和js 获取当前url各种属性

    转来 假设当前页完整地址是:http://www.test.com:80/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 " ...

  3. 9、搜索 :ion-searchbar

    /* ---html----*/ <ion-searchbar [(ngModel)]="searchQuery" (input)="getItems($event ...

  4. Hashtable元素的删除

    2中方法 Remove(); Clear(); static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1,& ...

  5. c# 判断是否是DICOM文件

    public bool isDicom(string filename) { FileStream fs = File.OpenRead(filename); ]; fs.Read(data, , d ...

  6. python 基础 知识

    Python Python 是一种强类型 的解释型 动态型语言Python 对象中的不可变 数字,字符串,元组 ,对于不能改变的会创建一个新的                可变  列表 , 字典   ...

  7. PAT 1030 Travel Plan

    #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #inc ...

  8. 【代码笔记】XML深入学习:DTD约束与DTD语法(1)

    2015-12-27 文件名    student.xml <?xml version="1.0" encoding="GB2312" standalon ...

  9. jQuery动态添加删除CSS样式

    jQuery框架提供了两个CSS样式操作方法,一个是追加样式addClass,一个是移除样式removeClass,下面通过一个小例子讲解用法. jQuery动态追加移除CSS样式 <!DOCT ...

  10. Ubuntu上使用vnc服务

    http://www.aichengxu.com/linux/8479752.htm 1. sudo apt-get install x11vnc 2. sudo x11vnc -storepassw ...