POJ 3616 奶牛挤奶

Milking Time

贝茜是一个勤劳的牛。事实上,她如此​​专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0..N-1),以便她生产尽可能多的牛奶。

农民约翰有一个M(1≤M≤1,000)可能重叠的间隔列表,他可以在那里进行挤奶。每个区间我有一个起始小时(0≤starting_houri≤N),一个结束小时(starting_houri <ending_houri≤N),以及相应的效率(1≤efficiencyi≤1,000,000),表示他可以从中获取多少加仑的牛奶。贝西在那段时间。 Farmer John分别在开始时间和结束时间开始时开始和停止挤奶。在挤奶时,Bessie必须在整个间隔内挤奶。

尽管贝茜有其局限性。在任何间隔期间挤奶后,她必须休息R(1≤R≤N)小时才能再次开始挤奶。鉴于Farmer Johns的间隔清单,确定Bessie在N小时内可以产生的最大牛奶量。

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 Nhour

题意:给了一段区间(开始,结束和价值),并给了了一个区间的间隔,求在N的时间内所能获得的最大价值;

题解:线性DP问题,刚开始思路有点问题,想对这总时间(N)直接进行dp,但是很难判定,而且会超时

子问题:在前i个区间内取得最大值,即对这m段的区间进行dp; 

划分问题:前i个可以从第j个(小于i)接上,和最长上升子序列是一样的模型; 

注意:这个输入数据对它先排个序(必须要排的,按照开始时间从小到大排);

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int INF=0x3f3f3f3f;
 8 const int maxn=1e3+5;
 9 
10 struct NOOD{
11     int s,e;
12     int val;
13 } nd[maxn];
14 
15 int f[maxn];
16 
17 bool cmp(NOOD x, NOOD y){
18     if(x.s==y.s)
19         return x.e<y.e;
20     return x.s<y.s;
21 }
22 
23 int main()
24 {
25     //freopen("in.txt", "r", stdin);
26     int n, m, r;
27     cin>>n>>m>>r;
28     for(int i=1; i<=m; i++)
29         cin>>nd[i].s>>nd[i].e>>nd[i].val;
30     sort(nd+1, nd+m+1, cmp);
31 
32     int ans=-INF;
33     for(int i=1; i<=m; i++)
34     {
35         f[i]=nd[i].val;      ///和最长上升子序列是一个模板
36         for(int j=1; j<i; j++)
37         {
38             if(nd[j].e+r<=nd[i].s)
39                 f[i]=max(f[i], f[j]+nd[i].val); ///选,不选
40         }
41         ans=max(ans, f[i]);
42     }
43     cout<<ans<<endl;
44     return 0;
45 }

猜你喜欢

转载自www.cnblogs.com/Yokel062/p/10772716.html