poj3616 Milking Time(区间dp)

一定要明确dp数组的定义再去做题!!!
题目大意:
  给定n个可能重叠的区间,对于每个区间,有区间端点和权值[a,b]权值为c。总区间范围为0~T。选择这些区间的一部分,求选取以后的区间的权值和的最大值。
 
                                                       Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14332   Accepted: 6071

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: NM, 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

 
 
菜鸡做dp几乎已经到了不看题解做不出来的地步了。。。(还是做题太少了,好多模型根本没见过)

这次做题主要知道了:一定要明确dp数组的定义再去做题!!!

dp[i]:在第i组数据时,1~i区间的最大产奶量(具体应该是,第1~i-1组数据最大的产奶量+第i组的产量)
或者说是,对于已经处理完了的前i-1组数据(设1<q<i-1,若cow[q].end_time>cow[i].start_time,则第q组称为为处理完)
可能不是很透彻emmmm不过水平有限,再往下解释不出来了
可以参考这个大哥的解释
所以对于dp[i],其初始值时dp[i]=第i组的产量(应该不用解释原因,很简单)
所以对于每个dp[i],要从1到i-1做一次循环,来判断该不该要第j个区间
cow[i].v为区间i的产量
dp[i]=max(dp[i],dp[j]+cow[i].v)
然后贴代码,其余细节写在注释
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     int st,et,v;
 9 }cow[1002];
10 int t,n,r,dp[1002];
11 bool cmp(node a,node b)
12 {
13     if(a.st==b.st) return a.et<b.et;
14     return a.st<b.st;
15 }
16 int main()
17 {
18     scanf("%d%d%d",&t,&n,&r);
19     for(int i=1;i<=n;i++)
20     {
21         scanf("%d%d%d",&cow[i].st,&cow[i].et,&cow[i].v);
22         cow[i].et+=r;//因为考虑间隔,所以直接把区间拉长
23     }
24     sort(cow+1,cow+1+n,cmp);
25     for(int i=1;i<=n;i++)
26     {
27         dp[i]=cow[i].v;
28         for(int j=1;j<i;j++)
29         {
30             if(cow[j].et<=cow[i].st)//首先保证上一个区间处理完了,如果上个区间还没结束,就开始计算这个区间,会重复计算,可以去掉这句用样例看一下
31             dp[i]=max(dp[i],dp[j]+cow[i].v);
32         }
33     }
34     //for(int i=1;i<=n;i++) printf("%d ",dp[i]);
35     printf("%d\n",*max_element(dp+1,dp+1+n));
36     /*选取最大的dp[i],可以试一下这样的样例:
37     100 2 2
38     1 100 99
39     5 6 1
40     就知道为什么了(还可以根据dp定义)
41     */
42     return 0;
43 }
View Code

猜你喜欢

转载自www.cnblogs.com/codeoosacm/p/9979114.html