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 Nhours
Sample Input
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
Sample Output
43
翻译:
贝茜是一个勤劳的牛。事实上,她如此专注于最大化她的生产力,于是她决定安排下一个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小时内可以产生的最大牛奶量。
思路:先按开始时间从小到大排序,dp[i]表示第i时间的最大,dp[i]=max(dp[j]+no[i].c,dp[i]);。类似最长上升子序列的DP思路。
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 1000000007 16 #define ll long long 17 using namespace std; 18 19 int n,m,r; 20 struct node 21 { 22 int a,b,c;//a开始时间,b结束时间,c效率。 23 }; 24 bool px(node q,node p)//按开始时间由小到大排序。 25 { 26 return q.a<p.a; 27 } 28 node no[1005]; 29 int dp[1005]; 30 int main() 31 { 32 scanf("%d %d %d",&n,&m,&r); 33 for(int i=0;i<m;i++) 34 { 35 scanf("%d %d %d",&no[i].a,&no[i].b,&no[i].c); 36 } 37 sort(no,no+m,px); 38 int ans=0; 39 for(int i=0;i<m;i++) 40 { 41 dp[i]=no[i].c; 42 for(int j=0;j<i;j++) 43 { 44 if(no[i].a>=no[j].b+r) 45 { 46 dp[i]=max(dp[j]+no[i].c,dp[i]); 47 } 48 } 49 if(ans<dp[i]) 50 { 51 ans=dp[i]; 52 } 53 } 54 printf("%d ",ans); 55 }