http://acm.hdu.edu.cn/showproblem.php?pid=1024
Max Sum plus Plus
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21363 Accepted Submission (s): 7144
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, S
2, S
3, S
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 INPUT1 3 1 2 32 6-1 4-2 3-2 3
Sample Output68 This problem in the country before the game I saw, but have never know how to start, finally decided to give it to a, now I can sink the heart, a good look at the analysis, good self to understand, sure enough still understand the focus on the recursive type
W[I][J]: The first j number is divided into I, the number of J must be selected; 1. The number of J is 1 paragraphs, 2. The number of J is connected with the previous number.
W[I][J] = max (b[i-1][j-1], w[i][j-1]) + a[j];
B[I][J]: The first j number is divided into I paragraph, the number of J can be optional; 1. The number of the selected J; 2. Do not select the number of J.
B[I][J] = max (b[i][j-1], w[i][k]);
W[j] means that J elements take the I segment, A[j] must take the maximum value
W[J] = max (dp[1-t][j-1], w[j-1]) + sum[j]-sum[j-1];
DP[T][J] indicates that the maximum value obtained in the a[j] is preferable to the two cases
DP[T][J] = max (dp[t][j-1], w[j]);
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#defineN 1000001using namespacestd;intSum[n], W[n], dp[2][n];///Sum[i] There are the first I andintMain () {intm, N; while(SCANF ("%d%d", &m, &n)! =EOF) { intI, J, X; sum[0] =0; for(i=1; i<=n; i++) {scanf ("%d", &x); Sum[i]= sum[i-1] +x; dp[0][i] =0;///take 0 segments from the previous I element and a maximum of 0 } /** We first assume that a[i] stored in the sequence of the first value, W[i][j] indicates that the first J number is divided into the I segment, the number of J must be selected in this case the maximum value obtained B[I][J] is the number of the first J to take the I paragraph The maximum value w[i][j]: The first j number is divided into I paragraph, the number of J must be selected; 1. The number of J is 1 paragraphs, 2. The number of J is connected with the previous number. W[I][J] = max (b[i-1][j-1], w[i][j-1]) + a[j]; B[I][J]: The first j number is divided into I paragraph, the number of J can be optional; 1. The number of the selected J; 2. Do not select the number of J. B[I][J] = max (b[i][j-1], w[i][k]); **/ intt=1; for(i=1; i<=m; i++)///I means take I segment { for(j=i; j<=n; j + +)///if Dp[i][j] (J<i) is meaningless. { if(i==j) Dp[t][j]= W[j] =Sum[j]; Else { ///W[j] means that J elements take the I segment, A[j] must take the maximum valueW[J] = max (dp[1-t][j-1], w[j-1]) + sum[j]-sum[j-1]; ///Dp[t][j] Indicates that the maximum value obtained in the a[j] is preferable to the two casesDP[T][J] = max (dp[t][j-1], w[j]); }} t=1-T;///T alternating between 0 and 1 straight /** Why Exchange??? This is to save space to carefully observe recursive w[i][j] = max (b[i-1][j-1], w[i][j-1]) + a[j]; B[I][J] = max (b[i][j-1], w[i][j]); We found that for the I segment, W[i][j] is only related to b[i-1][k-1] and w[i][k-1], and it has nothing to do with the previous items so our array can be smaller, overwriting the previous value with an update!!! **/} printf ("%d\n", dp[m%2][n]); } return 0;}
View Code
(Dynamic planning) Max Sum Plus plus--hdu--1024