Dynamic programming, given an integer array of length n (≤1e6) and an integer m, selects m consecutive and 22 disjoint sub-ranges, which makes the interval and maximum maximum in all schemes.
DP[I][J] Indicates the end position (the position of the last element in the last interval) is I and the maximum value of the interval number j is selected.
It is easy to get the following state transition equations:
and
Taking into account the size of the array and the updated features of J, a one-dimensional scrolling array is used instead of a two-dimensional array, and the outermost loop enumerates J to M.
Use Dp[0][i] to denote dp[i][j], dp[1][i] for Max (Dp[k][j-1]) (k≤i).
The complexity is O (n^2).
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 typedef __int64 LL;7 8 Const intINF =0x3f3f3f3f;9 Const intMAXN = 1e6 +Ten;Ten One intS[MAXN]; ALL dp[2][MAXN]; - LL ans, maxi; - intN, M; the - intMain () { - while(~SCANF ("%d%d", &m, &N)) { - for(inti =1; I <= N; i++) scanf ("%d", &s[i]); +Ans = Maxi =-inf; -Memset (DP,0,sizeofDP); + into =1; A for(intj =1; J <= M; J + +){ atMaxi =-inf; - for(inti = j; I <= N; i++){ -dp[0][i] = S[i] + max (dp[0][i-1], dp[1][i-1]); - } - for(inti = j; I <= N; i++) maxi = dp[1][i] = max (maxi, dp[0][i]); - } in for(inti = m; I <= N; i++) ans = max (ans, dp[0][i]); -printf"%i64d\n", ans); to } + return 0; -}
View Code
hdu1024 Max Sum plus Plus