Max sum plus
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 17164 accepted submission (s): 5651
Problem descriptionnow I think you have got an AC in Ignatius. l's "Max sum" problem. to be a brave acmer, we always challenge ourselves to more difficult problems. now you are 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 an integer m (M> 0), your task is to find m pairs of I and j which make sum (I
1, J
1) + Sum (I
2, J
2) + Sum (I
3, J
3) +... + Sum (I
M, J
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 write a special-Judge module, so you don't have to output m pairs of I and J, just output the maximal summation of sum (I
X, J
X) (1 ≤ x ≤ m) instead. ^_^
Inputeach test case will begin with two integers m and n, followed by N integers s
1, S
2, S
3... S
N.
Process to the end of file.
Outputoutput the maximal summation described abve in one line.
Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
Sample output
68HintHuge input, scanf and dynamic programming is recommended.
/*** DP [I] [J] indicates the maximum value that can be obtained by ending with the I number and dividing it into J portions, the transfer equation is ** DP [I] [J] = max (DP [I-1] [J], max (DP [1... i-1] [J-1]) + arr [I]; ** if the two-dimensional array is opened, the memory will exceed, so use the scrolling array to save space, premax [J] Save ** the DP [1... i] the maximum value in [J]. ans reads the maximum value of the current DP array every time ** to update the premax array. The answer saved by ans after the last round of loop is the answer. */# Include <stdio. h> # include <string. h> # define maxn 1000010 # define INF 0x7fffffint DP [maxn], premax [maxn], arr [maxn]; int max (int A, int B) {return A> B? A: B;} int main () {int n, m, I, j, ans; while (scanf ("% d", & N, & M) = 2) {for (I = 1; I <= m; ++ I) {scanf ("% d", & arr [I]); premax [I] = DP [I] = 0;} premax [0] = DP [0] = 0; For (j = 1; j <= N; ++ J) {// split into J portions ans =-INF; for (I = J; I <= m; ++ I) {DP [I] = max (DP [I-1], premax [I-1]) + arr [I]; premax [I-1] = ans; ans = max (ANS, DP [I]);} printf ("% d \ n", ANS);} return 0 ;}
Hdu1024 Max sum plus (DP]