POJ_3616_Milking_Time_(动态规划)

描述


http://poj.org/problem?id=3616

给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量.

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7507   Accepted: 3149

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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

Source

分析


对于每一次挤奶,结束时间+=休息时间.

先把m次挤奶按照开始时间排个序,用f[i]表示挤完第i个时间段的奶以后的最大挤奶量,那么有:

f[i]=max(f[i],f[j]+(第i次挤奶.ef)) (1<=j<i&&(第j次挤奶).ed<=(第i次挤奶).st).

注意:

1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因为不一定最后一次挤奶是哪一次).

 #include<cstdio>
#include<algorithm>
using namespace std; const int maxm=;
struct node
{
int st,ed,ef;
bool operator < (const node &a) const
{
return a.st>st;
}
}a[maxm];
int n,m,r;
int f[maxm]; void solve()
{
for(int i=;i<=m;i++)
{
f[i]=a[i].ef;
for(int j=;j<i;j++)
{
if(a[j].ed<=a[i].st)
{
f[i]=max(f[i],f[j]+a[i].ef);
} }
}
int ans=f[];
for(int i=;i<=m;i++) ans=max(ans,f[i]);
printf("%d\n",ans);
} void init()
{
scanf("%d%d%d",&n,&m,&r);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef);
a[i].ed+=r;
}
sort(a+,a+m+);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("milk.in","r",stdin);
freopen("milk.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
上一篇:json学习系列(7)JSONBuilder的用法


下一篇:google maps js v3 api教程(3) -- 创建infowindow