Test instructions The maximum value of the selected number by selecting the disjoint m segment from an array of n numbers
The upgraded version of Max Sum is not just a sequential selection but a M-segment idea or a similar DP
State and state transition equations it is not very difficult to think in the question of Max Sum, Dp[i] represents a section of max sum ending in number I, because there is also a state of how many segments here so that Dp[i][j] is selected in the first I number of J group and number I in the last Max Sum plus Plus in a group
So now there are two decisions for the number of I
1, the number of the first and the number of i-1 connected to a paragraph Dp[i][j] = Dp[i-1][j] + a[i]
2, Number I, I'm going to do it alone, so I need a j-1 section. Dp[i][j] = max{dp[k][j-1] | J-1<= K < i} + A[i]
then there's the state transition equation. Dp[i][j] = max (Dp[i-1][j],max{dp[k][j-1] | J-1<= K < i}) + a[i];
With the equation, we can write it. Let's say we don't think about complexity first, assuming that the time space is infinite.
Then it's easy to have the following code
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 10005, INF = 1e9;int A[n], Dp[n][n];int main () { int N, m; while (~SCANF ("%d%d", &m, &n)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]); memset (DP, 0, sizeof DP); DP[I][J] Indicates the number of first I number I is selected j Group Max Sum plus plus //dp[i][j] = max (Dp[i-1][j], max{dp[k][j-1], j-1 <= K < i}) + a[ I] int ans =-inf; for (int j = 1; J <= M, J + +) {for (int i = j; i <= N; i++) { Dp[i][j] = i-1 < J?-inf:dp[ I-1][J]; I-1 < J Dp[i-1][j] is meaningless for (int k = j-1; k < i; ++k) dp[i][j] = max (Dp[i][j], dp[k][j-1]); DP[I][J] + = A[i]; if (j = = m) ans = max (ans, dp[i][j]);} } printf ("%d\n", ans); } return 0;}
But it's not infinite. Above this time complexity 0 (n*n*m) space complexity O (n*n) n up to 1000000 is simply horrible
and look at the transfer equation. Dp[i][j] = max (Dp[i-1][j], max{dp[k][j-1] | J-1<= K < i}) + a[i];
Found updating dp[i][j] only when the dp[was used. [j] and dp[.] [J-1] The value inside is also dp[. [0~j-2] is useless, so it doesn't have to be useless. That is, J just open two-dimensional is enough, so-called rolling array with dp[. [1] and dp[.] [0] To rotate the expression dp[.] [j] and dp[.] [J-1] So the space complexity is reduced to O (n) which is acceptable then the space-optimized code
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 1000005, INF = 1e9;int A[n], Dp[n][2];int main () {int N, m; while (~SCANF ("%d%d", &m, &n)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]); memset (DP, 0, sizeof DP); DP[I][J] Indicates the number of first I number I is selected j Group Max Sum plus plus//dp[i][j] = max (Dp[i-1][j], max{dp[k][j-1], j-1 <= K < i}) + A[i] int ans =-inf, j0 = 0, J1 = 1; for (int j = 1; j <= M; j + +) {for (int i = j; i <= N; i++) {//dp[i][j0] Save The value of Dp[i][j-1] dp[i][j1] is the value of dp[i][j] dp[i][j1] = I-1 < J? -INF:DP[I-1][J1]; I-1 < J Dp[i-1][j] is meaningless for (int k = j-1; k < i; ++k) dp[i][j1] = max (dp[i][j 1], dp[k][j0]); DP[I][J1] + = A[i]; if (j = = m) ans = max (ans, dp[i][j1]); } swap (J0, J1); Scrolling array Exchange 0, 1 Because this round of j in the lower turn becomes j-1} printf ("%d\n", ans); } return 0;}
Now that the space problem solves that time problem, look at this code.
<span style= "White-space:pre" ></span>for (int j = 1; j <= M; j + +) <span style= "White-space:pre" ></span>{for (int i = j; i <= N; i++) { // dp[i][j1] = i-1 < J-inf:dp[i-1][j1];// for (int k = j-1; k < i; ++k) dp[i][j1] = max (dp[i][j1], dp[k][j0]);//This cycle is only a dp[i-1][j0]!!! more than the wheel dp[i][j1] + = A[i]; if (j = = m) ans = max (ans, dp[i][j1]); } Swap (j0, J1); // }
Look at the notes section!!! This round is only a few more than the last number I still circulate so much is what the Devil put the value Ma on the wheel to record this round only to compare with dp[i-1][j0] Then there is the code that can be AC
< Span style= "font-family: ' Microsoft Yahei '; font-size:14px ">
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 1000005, INF = 1e9;int A[n], Dp[n][2];int main () {int N, m; while (~SCANF ("%d%d", &m, &n)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]); memset (DP, 0, sizeof DP); DP[I][J] Indicates the number of first I number I is selected j Group Max Sum plus plus//dp[i][j] = max (Dp[i-1][j], max{dp[k][j-1], j-1 <= K < i}) + A[i] int ans =-inf, j0 = 0, J1 = 1; for (int j = 1; j <= M; j + +) {int ma = dp[j-1][j1] =-inf;//dp[j-1][j] is meaningless for (int i = J; I <= N; i++) {//dp[i][j0] has a value of dp[i][j-1] dp[i][j1] The value of dp[i][j] ma = Max (MA, dp[i-1][j 0]); MA maintenance max{dp[k][j-1], j-1 <= k < i} dp[i][j1] = max (dp[i-1][j1], MA) + a[i]; if (j = = m) ans = max (ans, dp[i][j1]); } swap (J0, J1); Scrolling array Exchange 0, 1 because this round of J turns into j-1 at the turn of the wheel} printf ("%d\n", ans); } return 0;}
Max Sum plus Plus
Problem Descriptionnow I Think you have got a AC in IGNATIUS.L ' s "Max Sum" problem. To is a brave acmer, we always challenge ourselves to more difficult problems. Now you is faced with a more difficult problem.
Given a consecutive number sequence S
1-2
2-2
3-2
4... S
x, ... S
N(1≤x≤n≤1,000,000, -32768≤s
x≤32767). We define a function sum (i, j) = S
I+ ... + S
J(1≤i≤j≤n).
Now given a integer m (M > 0), your task is to find m pairs of I and J which make sum (i
1J
1) + SUM (i
2J
2) + SUM (i
3J
3) + ... + sum (i
mJ
m) Maximal (i
x≤i
y≤j
xOr I
x≤j
y≤j
xis not allowed).
But I ' m lazy, I don't want to the write a Special-judge module, so you don ' t has to output m pairs of I and j, just output th e maximal summation of sum (i
xJ
x) (1≤x≤m) instead. ^_^
Inputeach test case would begin with a integers m and n, followed by n integers S
1+ t
2+ t
3... S
N.
Process to the end of file.
Outputoutput the maximal summation described above in one line.
Sample Input
1 3 1 2 32 6-1 4-2 3-2 3
Sample Output
HintHuge input, scanf and dynamic programming is recommended.
Authorjgshining (Aurora Dazzle)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU Max Sum plus Plus (DP scrolling array)