【BZOJ】1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1642
果然没能仔细思考是不行的。。
以后要静下心来好好想,不要认为不可做。。。。。
看了题解。。。
首先按开始时间排序,然后
f[i]表示前i个最多能够挤得的奶,那么有
f[i]=w[i],初始化
f[i]=max(f[i], f[j]+w[i]) 当j的结束时间+R不超过i的开始时间
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const int N=1005;
struct nod { int s, e, w; }a[N];
int n, r, m, f[N];
bool cmp(const nod &a, const nod &b) { return a.s<b.s; }
int main() {
read(n); read(m); read(r);
for1(i, 1, m) read(a[i].s), read(a[i].e), read(a[i].w);
sort(a+1, a+1+m, cmp);
int ans=0;
for1(i, 1, m) {
f[i]=a[i].w;
for1(j, 1, i-1) if(a[i].s>=a[j].e+r && f[i]<f[j]+a[i].w) f[i]=f[j]+a[i].w;
ans=max(ans, f[i]);
}
print(ans);
return 0;
}
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
注意:结束时间不挤奶
Source
博客地址:www.cnblogs.com/iwtwiioi 本文为博主原创文章,未经博主允许不得转载。一经发现,必将追究法律责任。

浙公网安备 33010602011771号