1642: [Usaco2007 Nov]Milking Time 挤奶时间
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 525 Solved: 300
[Submit][Status]
Description
贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量。为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1。 Farmer John 计划好了 M (1 ≤ M ≤ 1,000) 个可以挤奶的时间段。每个时间段有一个开始时间(0 ≤ 开始时间 ≤ N), 和一个结束时间 (开始时间 < 结束时间 ≤ N), 和一个产量 (1 ≤ 产量 ≤ 1,000,000) 表示可以从贝茜挤奶的数量。Farmer John 从分别从开始时间挤奶,到结束时间为止。每次挤奶必须使用整个时间段。 但即使是贝茜也有她的产量限制。每次挤奶以后,她必须休息 R (1 ≤ R ≤ N) 个小时才能下次挤奶。给定Farmer John 计划的时间段,请你算出在 N 个小时内,最大的挤奶的量。
Input
第1行三个整数N,M,R.接下来M行,每行三个整数Si,Ei,Pi.
Output
最大产奶量.
Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43
HINT
注意:结束时间不挤奶
题解:
这种题目边界问题最令人蛋疼。。。
类似于wikioi上的线段覆盖,按起点排序然后DP
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 500+100
#define maxm 1000+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
ll ans,f[maxm];
struct rec{int s,e,p;}a[maxm];
int n,m,r;
inline bool cmp(rec a,rec b)
{
return a.s<b.s;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();r=read();
for(int i=;i<=m;i++)a[i].s=read(),a[i].e=read()+r,a[i].p=read();
sort(a+,a+m+,cmp);
for(int i=;i<=m;i++)
{
f[i]=a[i].p;
for(int j=;j<=i-;j++)
if(a[j].e<=a[i].s)f[i]=max(f[i],f[j]+a[i].p);
ans=max(ans,f[i]);
}
printf("%lld\n",ans);
return ;
}