poj3616(Milking Time)
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
先对区间左端点从小到大排序,相同时右端点小的在前。然后从前往后依次处理,设dp[i]表示选第i个区间可以获得的最大值,那么对i之前任意与i相距大于等于r的区间j,dp[i]=max(dp[i],dp[j]+s[i].w)
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
struct milk
{
int x, y, w;
bool operator < (const milk& b) const
{
if (x == b.x)
return y < b.y;
return x < b.x;
}
};
int main()
{
int n, m, r, dp[1005];
milk s[1005];
cin >> n >> m >> r;
for (int i = 0; i < m; ++i)
scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].w);
sort(s, s+m);
for (int i = 0; i < m; ++i)
dp[i] = s[i].w;
int ans = 0;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < i; ++j)
if (s[i].x >= s[j].y + r)
dp[i] = max(dp[i], dp[j]+s[i].w);
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
return 0;
}
公众号「算法码上来」。godweiyang带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。