Milking Time(DP)
个人心得:一开始自己找状态,是这么理解的,只要前面一个满足就等于此时的值加上d(n-1),否则就是不挖此时的比较d(n-1)和
d(n-2)+cost,不过仔细一想忽略了很多问题,你无法确定n-2和此时的n是否可以一起挖,同时跳跃性的递归无法比较,
后面参考了网上的递推,他是确定这个n必须挖的最大值,则n前面的最大值+cost进行比较,仔细一想,这样是能求出挖这个
地方的最大值,所以后面输出就还要进行一轮最大值判断
1 for(int i=0;i<m;i++){ 2 d[i]=C[i].earn; 3 for(int j=0;j<i;j++) 4 { 5 if(C[j].ends<=C[i].start) 6 d[i]=max(d[i],d[j]+C[i].earn); 7 8 } 9 }
注意了,它上面的代表着是D此时的值,即挖i的最大值,因为满足无后续性,所以内循环可以从0到i,确定是否挖与不挖。多想想
Language:
Milking Time
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 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 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iomanip> 6 #include<string> 7 #include<algorithm> 8 using namespace std; 9 struct cow 10 { 11 int start,ends; 12 int earn; 13 }C[1005]; 14 int n,m,r; 15 bool cmp(cow a,cow b){ 16 if(a.start==b.start) 17 return a.ends<b.ends; 18 return a.start<b.start; 19 } 20 int d[1005]; 21 int main(){ 22 cin>>n>>m>>r; 23 for(int i=0;i<m;i++){ 24 cin>>C[i].start>>C[i].ends>>C[i].earn; 25 C[i].ends+=r; 26 } 27 sort(C,C+m,cmp); 28 for(int i=0;i<m;i++){ 29 d[i]=C[i].earn; 30 for(int j=0;j<i;j++) 31 { 32 if(C[j].ends<=C[i].start) 33 d[i]=max(d[i],d[j]+C[i].earn); 34 35 } 36 } 37 int maxn=0; 38 for(int i=0;i<m;i++) 39 maxn=max(maxn,d[i]); 40 cout<<maxn<<endl; 41 return 0; 42 }
|