Max Sum plus Plus
Test instructions: Test instructions understand long time, here is said in the given series, take M group of sub-series, can not repeat, make these sub-sequence and maximum;
Just like m=2 time, 1/2/-4/5/6. Can not take-4 meaning;
Idea: The idea of this problem is dynamic programming, recursive;
State Dp[i][j] Indicates the maximum number of the first J and the largest of the group I. Decision: The number of J, either contained in group I, or independent group. Where the last group contains a[j]. (This is critical)
Then the state transition equation is: (in a two-dimensional graph, it is either taken from the left or the maximum value of the row, the left Max is contained in group I, the right side Max is independent group)
DP[I][J]=MAX{DP[I][J-1]+A[J],MAX{DP[I-1][T]}+A[J]} i-1=<t<j-1
This problem n data is too large, the two-dimensional array is not open, and the triple loop, thinking of the state transition equation is still difficult.
Think of the two-dimensional array if it doesn't work, it must be compressed into one-dimensional arrays:
Because the value of dp[i-1][t] is only used when calculating dp[i][j], it is not necessary to save all Dp[i][j] for I=1 to M, so that we can store it in a one-dimensional array.
Use pre[j] to represent the maximum field between J before a state DP[i-1][] 1-j and (not necessarily including a[j]), and then push DP [i][j] state, DP[i][j]=MAX{PR E[J-1],DP[J-1]}+A[J];
Red for the convenience of understanding, in fact, does not exist.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<string>using namespacestd;Const intMAXN =1000000+7;Const intINF =0x3f3f3f;intPRE[MAXN],DP[MAXN],A[MAXN];intn,m;intMain () { while(~SCANF ("%d%d",&m,&N)) { for(intI=1; i<=n;i++) scanf ("%d",&A[i]); dp[0]=0; memset (PRE,0,sizeof(pre)); intMmax; for(intI=1; i<=m;i++) {Mmax= -inf; for(intj=i;j<=n;j++)//for each I, with the increase of J, the Maxx is bigger, (greedy for continuous and maximum{Dp[j]= Max (dp[j-1],pre[j-1])+A[j]; Pre[j-1] = Mmax;//assign the maximum value of the previous round (j-1) to the pre;//Note that this is the second sentence, because the pre can only work in the (i+1) round;Mmax =Max (mmax,dp[j]); }} printf ("%d\n", Mmax);//the maximum value of the last round is the answer. } return 0;}//give a set of data, do not understand to put all the DP out, self-simulation to do it again, so good understanding/*4 2-4 5 6-8*/
Hdu-10240max Sum Plus plus+ Dynamic planning + scrolling Array