您的位置:首页 > 其它

poj 3616 Milking Time 【dp】

2015-12-20 15:01 309 查看
Milking Time

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6534 Accepted: 2745
Description

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


题意:有M个产奶工作,已经给出每个工作的时间范围和获得的牛奶数。在经历一个产奶工作之后,要休息R小时才可以继续下一个工作。问你在[1, N]的时间内可以获得的最大牛奶数。

思路:dp[i]表示前i个产奶工作且第i个产奶工作完成 在[1, N]时间内可以获得的最大牛奶数。

状态转移

dp[i] = max(dp[j]) + num[j].v  (1 <= j < i && num[j].t + R <= num[i].s)。

最后维护一下dp[i](1<=i<=M)就可以了。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (1000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Node{
int s, t, v;
};
Node num[MAXN];
bool cmp(Node a, Node b){
if(a.s != b.s)
return a.s < b.s;
else if(a.t != b.t)
return a.t < b.t;
else
return a.v > b.v;
}
int dp[1010];
int main()
{
int N, M, R;
while(scanf("%d%d%d", &N, &M, &R) != EOF)
{
for(int i = 1; i <= M; i++)
Ri(num[i].s), Ri(num[i].t), Ri(num[i].v);
sort(num+1, num+M+1, cmp);
CLR(dp, 0);
for(int i = 1; i <= M; i++)
{
dp[i] = num[i].v;
for(int j = 1; j < i; j++)
if(num[j].t + R <= num[i].s)
dp[i] = max(dp[i], dp[j] + num[i].v);
}
int ans = 0;
for(int i = 1; i <= M; i++)
ans = max(ans, dp[i]);
Pi(ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: