poj 3616 Milking Time
Posted ZefengYao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3616 Milking Time相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8048 | Accepted: 3388 |
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
题意:在[1,N]的区间上,包含许多子区间,在每个子区间里农夫约翰可以给牛挤奶,每段子区间牛产奶量也有区别,并且一旦在某个区间挤了奶,牛都要休息一定时间才能继续挤奶.共有M个挤奶区间,求最多能挤多少奶。
思路:动态规划,dp[i]意义:到第i个挤奶区间为止能挤到的最多的牛奶量。
任取整数k属于[1,i-1],每个区间k的end时间都小于区间i的开始时间,那么到第i个挤奶区间为止能挤到的最多的牛奶量等于max{第k个区间为止能挤到的最多的牛奶量+第i个区间能挤到的牛奶量},即dp[i]=max{dp[k]+interval[i].efficiency},若找不到任何一个k,dp[i]=interval[i].efficiency
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> using namespace std; const int N_MAX = 1001; struct interval { int begin, end, efficiency; bool operator <(const interval&b)const { return begin < b.begin; } }; interval Interval[N_MAX]; int dp[N_MAX];//dp[i]代表i个区间及之前的区间牛生产牛奶的最大值 int main() { int N,M,R; while (cin >> N>>M>>R) { for (int i = 0;i < M;i++) { scanf("%d%d%d", &Interval[i].begin, &Interval[i].end, &Interval[i].efficiency); Interval[i].end += R;//每个区间结束时间相当于加上休息时间 } sort(Interval,Interval+M); for (int i = 0;i < M;i++) { dp[i] = Interval[i].efficiency; for (int j = 0;j < i;j++) { if (Interval[j].end<= Interval[i].begin) { dp[i] = max(dp[i], dp[j] + Interval[i].efficiency);//前面(i-1)个区间中任意一个和当前区间不重叠的区间k } //dp[i]就是所有dp[k]+Interval[i].efficiency中的最大值 } } cout << *max_element(dp, dp + M) << endl; } return 0; }
以上是关于poj 3616 Milking Time的主要内容,如果未能解决你的问题,请参考以下文章