BZOJ1642: [Usaco2007 Nov]Milking Time 挤奶时间

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

Sample Output

43

HINT

注意:结束时间不挤奶

题解:
这种题目边界问题最令人蛋疼。。。
类似于wikioi上的线段覆盖,按起点排序然后DP
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 500+100
14 #define maxm 1000+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 inline int read()
20 {
21     int x=0,f=1;char ch=getchar();
22     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
23     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
24     return x*f;
25 }
26 ll ans,f[maxm];
27 struct rec{int s,e,p;}a[maxm];
28 int n,m,r;
29 inline bool cmp(rec a,rec b)
30 {
31     return a.s<b.s;
32 }
33 int main()
34 {
35     freopen("input.txt","r",stdin);
36     freopen("output.txt","w",stdout);
37     n=read();m=read();r=read();
38     for(int i=1;i<=m;i++)a[i].s=read(),a[i].e=read()+r,a[i].p=read();
39     sort(a+1,a+m+1,cmp);
40     for(int i=1;i<=m;i++)
41      {
42          f[i]=a[i].p;
43          for(int j=1;j<=i-1;j++)
44           if(a[j].e<=a[i].s)f[i]=max(f[i],f[j]+a[i].p);
45          ans=max(ans,f[i]); 
46      } 
47     printf("%lld
",ans); 
48     return 0;
49 }
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/3942262.html