Given n numbers, the maximum value of the M segment (no continuous between segments and segments, but continuous in a period)
For example:2 5 1-2 2 3-1 Five digits Select 2, choose 1 and 2 3 for each of the two paragraphs.
DP[I][J] Select I in the former J number, and then according to the J number is independent into a paragraph, you can write
State transition equation:dp[i][j]=max (Dp[i][j-1]+num[j],max (dp[i-1][k)) +num[j])
Here Max (Dp[i-1][k]) stands for the maximum value of the I-1 segment, and then adds Num[j] as a separate paragraph.
But the problem does not give the range of M, it is possible to burst memory and explosion, you need to deal with.
For explosion-proof Memory: note dp[i][*] only and dp[i][*],dp[i-1][*], that is, the current state is only related to the previous state, you can use the scrolling array optimization (data).
For explosion protection: since Max (Dp[i-1][k] represents the maximum value of the i-1 segment, we can use an array pre to store the maximum value before
State transition equation: Dp[i][j]=max (dp[i][j-1]+num[j],pre[j-1]+num[j]) Find out what I'm doing, so
The final state transfer equation:dp[j]=max (Dp[j-1]+num[j],pre[j-1]+num[j])
#include <iostream>#include<algorithm>using namespacestd;Const intn=1000010;Const intinf=0x3f3f3f3f;intNum[n],pre[n],dp[n];intMain () {intn,m; while(SCANF ("%d%d", &m,&n)! =EOF) { for(intI=1; i<=n;i++) scanf ("%d", &num[i]), dp[i]=0, pre[i]=0; intMAX; dp[0]=pre[0]=0; for(intI=1; i<=m;i++) {MAX=-INF; for(intj=i;j<=n;j++) {//I start with I, because I need at least a number to support I segmentDp[j]=max (dp[j-1]+num[j],pre[j-1]+Num[j]); Pre[j-1]=MAX; MAX=Max (max,dp[j]); }} printf ("%d\n", MAX); } return 0;}
HDU-1024 Max Sum plus plus scroll array optimization