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 Nhours
Sample Input
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
Sample Output
43
思路:
先把数据排序,按开始时间从小到大排一下,用dp[i]表示选择下标为i的时间从0到i 挤奶量的最大值
dp[i] = max(dp[i], dp[j] + a[i].v) 条件 : a[i].st - k >= a[j].en
AC代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>
typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;
using namespace std;
struct node
{
int st,en,v;
}a[maxn];
int dp[maxn];
bool cmp(node a,node b)
{
if(a.st == b.st)
return a.en<b.en;
return a.st<b.st;
}
int main()
{
int n,m,k;
int res = 0;
sca3(n,m,k);
rep(i,0,m)
{
sca3(a[i].st, a[i].en, a[i].v);
}
sort(a,a+m,cmp);
rep(i,0,m)
{
dp[i] = a[i].v;
rep(j,0,i)
{
if(a[i].st - k >= a[j].en)
dp[i] = max(dp[i], dp[j] + a[i].v);
}
res = max(res,dp[i]);
}
pri(res);
return 0;
}