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
Inputcopy | Outputcopy |
---|---|
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31 | 43 |
原题链接: 传送门
题意:
贝茜是一个勤劳的牛。事实上,她如此专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0..N-1),以便她生产尽可能多的牛奶。
农民约翰有一个M(1≤M≤1,000)可能重叠的间隔列表,他可以在那里进行挤奶。每个区间我有一个起始小时(0≤starting_houri≤N),一个结束小时(starting_houri <ending_houri≤N),以及相应的效率(1≤efficiencyi≤1,000,000),表示他可以从中获取多少加仑的牛奶。贝西在那段时间。 农民约翰分别在开始时间和结束时间开始时开始和停止挤奶。在挤奶时,贝茜必须在整个间隔内挤奶。
尽管贝茜有其局限性。在任何间隔期间挤奶后,她必须休息R(1≤R≤N)小时才能再次开始挤奶。鉴于农民约翰的间隔清单,确定贝茜在N小时内可以产生的最大牛奶量。
思路:
定义状态表示为以第i段时间作为最后一段时间时的总产量。
把休息的时间加到每段结束的时间,然后用初始时间去更新
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
struct node{int s, e, c;}a[1000009];
bool cmp(node i, node j){return i.s < j.s;}
int n, m, r, i, j, ans, f[1000009];
signed main(){
scanf("%d%d%d", &n, &m, &r);
for(i = 1; i <= m; i++) cin >> a[i].s >> a[i].e >> a[i].c, a[i].e += r;
sort(a + 1, a + 1 + m, cmp);
ans = f[1] = a[1].c;
for(i = 2; i <= m; i++){
for(j = i - 1, f[i] = a[i].c; j >= 1; j--)
if(a[j].e <= a[i].s && f[j] + a[i].c > f[i])
f[i] = f[j] + a[i].c;
ans = max(ans, f[i]);
}
cout << ans << "\n";
return 0;
}