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:

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output:

43

题目链接

先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,然后用动态规划做。

dp[i]代表第i个时间区间挤奶可获得的最大产奶量,需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,求出最大值加上第i个时间区间的产奶量就是dp[i],最后去dp数组最大值即可。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <stdlib.h>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <bitset>
#include <complex>
#include <functional>
#include <fstream>
#include <ctime>
#include <stdexcept>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
inline void fre() {freopen("in.txt", "r", stdin);/*freopen("out.txt", "w", stdout);*/}

struct ac {
	int s, e, eff;
	bool operator < (const ac &a) const {
		return e < a.e;
	}
};

int main() {
// fre();
	int n, m, r;
	read(n); read(m); read(r);
	vector<ac> a(m + 1);
	a[0].s = 0; a[0].e = 0; a[0].eff = 0;
	for (int i = 1; i <= m; ++i) {
		read(a[i].s); read(a[i].e); read(a[i].eff);
		a[i].e += r;
	}
	sort(a.begin(), a.end());
	vector<int> dp(m + 1);
	dp[0] = 0;
	int ans = 0;
	for (int i = 1; i <= m; ++i) {
		dp[i] = 0;
		for (int j = 0; j < i; ++j) {
			if (a[j].e <= a[i].s) {
				dp[i] = max(dp[i], dp[j] + a[i].eff);
			}
		}
		if (dp[i] > ans) {
			ans = dp[i];
		}
	}
	printf("%d\n", ans);
    return 0;
}