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
题意:我就不说了,语文不好说不清楚。
题解:贪心,至于怎么贪心看代码吧,但是我看别人代码是直接return A.l<B.l;反正也过了,排完序后,再按照普通的dp写就出来了
bool cmp(node A,node B)
{
if(A.r!=B.r)
{
return A.r<B.r;
}
else
{
if(A.l!=B.l)
{
return A.l<B.r;
}
else
{
return A.w>B.w;
}
}
}
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define se second
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 3e5 + 50;
const int maxm = 1.5e5+50;
const double eps = 1e-7;
const double inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const double pi=3.141592653589;
struct node
{
int l,r,w;
}a[1005];
bool cmp(node A,node B)
{
if(A.r!=B.r)
{
return A.r<B.r;
}
else
{
if(A.l!=B.l)
{
return A.l<B.r;
}
else
{
return A.w>B.w;
}
}
}
int dp[1005];
int main()
{
int n,m,r;
scanf("%d%d%d",&n,&m,&r);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].w);
a[i].r+=r;
}
sort(a+1,a+1+m,cmp);
int ans=0;
for(int i=1;i<=m;i++)
{
dp[i]=a[i].w;
for(int j=1;j<i;j++)
{
if(a[i].l>=a[j].r)
dp[i]=max(dp[i],dp[j]+a[i].w);
ans=max(dp[i],ans);
}
}
printf("%d\n",ans);
return 0;
}