POJ 3616 Milking Time

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3607   Accepted: 1533

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

 

动态规划问题,用一个数组dp[i]记录到第i小时最大的产奶量

先将所有牛的产奶时间按结束时间由小到大排序

然后将i从0到n遍历递推一遍,每次先将dp[i]初始化为dp[i-1],然后遍历所有结束时间与i相等的奶牛

  dp[i]=max(dp[i],dp[time[t].start_hour-r]+time[t].eff)

注意time[t].start_hour-r<0时,递推式是这样的:

  dp[i]=max(dp[i],time[t].eff)

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 
 6 using namespace std;
 7 
 8 typedef struct
 9 {
10     int start_hour;
11     int end_hour;
12     int eff;
13 } INTERVAL;
14 
15 int n,m,r;
16 int dp[1000010];
17 INTERVAL time[1010];
18 
19 bool cmp(INTERVAL a,INTERVAL b)
20 {
21     return a.end_hour<b.end_hour;
22 }
23 
24 int main()
25 {
26     while(scanf("%d %d %d",&n,&m,&r)==3)
27     {
28         for(int i=0;i<m;i++)
29             scanf("%d %d %d",&time[i].start_hour,&time[i].end_hour,&time[i].eff);
30         sort(time,time+m,cmp);
31 
32         int t=0;
33         dp[0]=0;
34 
35         for(int i=1;i<=n;i++)
36         {
37             dp[i]=dp[i-1];
38             while(t<m&&time[t].end_hour==i)
39             {
40                 if(time[t].start_hour-r<0)
41                     dp[i]=max(dp[i],time[t].eff);
42                 else
43                     dp[i]=max(dp[i],dp[time[t].start_hour-r]+time[t].eff);
44                 t++;
45             }
46         }
47         cout<<dp[n]<<endl;
48     }
49 
50     return 0;
51 }
[C++]

 

posted @ 2013-08-21 19:51  ~~Snail~~  阅读(205)  评论(0编辑  收藏  举报