博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3616 Milking Time(简单DP)
阅读量:5325 次
发布时间:2019-06-14

本文共 2778 字,大约阅读时间需要 9 分钟。

Milking Time

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 3055

 

Accepted: 1281

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

 解题报告:这道题就是怎么安排牛的顺序才能效率最大,如果没有效率的话,感觉有点像活动安排,但是加了效率,就得用数组记录记录当前的状态了,自然想到了用DP,先按活动的结束时间从小到大排序,再动态规划即可!

代码如下:

#include 
#include
#include
#include
#include
#define Max(a, b)(a > b ? a : b)using namespace std;const int MAX = 1010;int dp[MAX];struct Cow//记录牛的信息{ int start; int end; int eff;}cow[MAX];int cmp(const void *a, const void *b)//结构体一级排序,按结束时间从小到大排序{ return (*(Cow *)a).end - (*(Cow *)b).end;}int N, M, R;int main(){ int i, j; scanf("%d%d%d", &N, &M, &R); for (i =0; i < M; ++i) { scanf("%d%d%d", &cow[i].start, &cow[i].end, &cow[i].eff); } qsort(cow, M, sizeof(cow[0]), cmp); for (i = 0; i < M; ++i)//初始化 { dp[i] = cow[i].eff; } for (i = 0; i < M; ++i) { for (j = i + 1; j < M; ++j) { if (cow[j].start >= cow[i].end + R) { dp[j] = Max(dp[i] + cow[j].eff, dp[j]); } } } int ans = 0; for (i = 0; i < M; ++i)//找到最大的 { if (ans < dp[i]) { ans = dp[i]; } } printf("%d\n", ans); return 0;}

转载于:https://www.cnblogs.com/lidaojian/archive/2012/05/10/2495048.html

你可能感兴趣的文章
stm32中字节对齐问题(__align(n),__packed用法)
查看>>
like tp
查看>>
posix多线程有感--线程高级编程(线程属性函数总结)(代码)
查看>>
spring-使用MyEcilpse创建demo
查看>>
DCDC(4.5V to 23V -3.3V)
查看>>
kettle导数到user_用于left join_20160928
查看>>
activity 保存数据
查看>>
typescript深copy和浅copy
查看>>
linux下的静态库与动态库详解
查看>>
hbuilder调底层运用,多张图片上传
查看>>
深入理解基于selenium的二次开发
查看>>
较快的maven的settings.xml文件
查看>>
Git之初体验 持续更新
查看>>
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
查看>>
随手练——HDU 5015 矩阵快速幂
查看>>
启动redis一闪就关
查看>>
Maven之setting.xml配置文件详解
查看>>
ASP.NET 4.5 Web Forms and Visual Studio vs2013年入门1
查看>>
SDK目录结构
查看>>
malloc() & free()
查看>>