The main effect of the topic:
The problem is that a farmer is milking a cow in order to get the maximum amount of milk produced. Given the n hours of the day, and then the m time period can be milked, every time the milking, the cows will rest r hours, milking every period of
Efficiency is efficiency_i. The largest amount of milking is obtained.
Ideas for solving problems:
This problem, think about greed should be able to, for all the coordinates, find the first end of the point, and then do not meet the overlap in the case to calculate the maximum milk, but this is sometimes the contradiction between greed and DP,
Because I'm looking for the first milking stage, and then the same way to find the next one, it will cause n such a phase and less than a certain value, so this greedy algorithm can not complete the optimal solution of the problem.
Then it comes to the idea of using a DP, defined state Dp[i] to represent the maximum amount of milk that is obtained from the previous phase.
State transition equation: dp[i] = dp[i-1]+a[j].efficiency;
Well, with this equation, just like solving other DP problems, the final output is OK.
Code:
# include<cstdio> # include<iostream> # include<algorithm> # include<cstring> # include< string> # include<cmath> # include<queue> # include<stack> # include<set> # Include<map
> Using namespace std;
# define INF 999999999 # define MAX 1000+4 int Dp[max];
int n,m,r;
struct Node {int sta;
int en;
int eff;
}a[max];
int CMP (const node & x,const node & y) {return x.sta < Y.sta;}
int main (void) {while (cin>>n>>m>>r) {for (int i = 0;i < m;i++) {
cin>>a[i].sta>>a[i].en>>a[i].eff;
A[i].en + = r;
Sort (a,a+m,cmp);
for (int i = 0;i < m;i++) Dp[i] = A[i].eff; for (int i = 0;i < m;i++) {for (int j = 0;j < i;j++) {if (a[j ].en <= A[i].sta) {Dp[i] = max (dp[i],dp[j]+a[i].eff);
'} int ans = 0;
for (int i = 0;i < m;i++) {ans = max (ans,dp[i]);
} cout<<ans<<endl;
return 0;
}