Problem Description:now 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
1+ M
1) + SUM (i
2+ M
2) + SUM (i
3+ M
3) + ... + sum (i
m+ M
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
x+ M
x) (1≤x≤m) instead. ^_^ Input:each test case would begin with the integers m and n, followed by n integers S
1+ t
2+ t
3... S
N.
Process to the end of file. Output:output the maximal summation described above in one line. Sample Input:1 3 1 2 6-1 4-2 3-2 3 Sample output:68
HintHuge input, scanf and dynamic programming is recommended.Test Instructions: There is a sequence of length n, now to be divided into a non-intersecting m-segment sequence (without dividing all the elements), ask how to make this M-segment sequence and maximum, the problem does not seek this M-segment is what, only the output of this maximum value. Suppose we let the array a[j] represent the input array, dp[i][j] means that the first J number is divided into the maximum of the group I (without dividing all the first J elements), then we must understand: the number of J may be divided into a group of independent, may also be added to the I-1 group, becomes the element of the I-1 group, so the state transition equation becomes dp[j] = max (Dp[j-1]+a[j], max[j-1]+a[j]) (because if the two-dimensional array memory is too large, we can use the double for loop to calculate the maximum value, At this point only two one-dimensional arrays are required.
#include <stdio.h>#include<algorithm>#include<string.h>using namespacestd;Const intn=1000010;Const intinf=0x3f3f3f3f;intA[n], Dp[n], max[n];///Dp[j] Maximum value of the pre-set J number of packets, max[j-1] holds the maximum value of the previous groupintMain () {intm, N, I, J, M; while(SCANF ("%d%d", &m, &n)! =EOF) { for(i =1; I <= N; i++) scanf ("%d", &A[i]); Memset (DP,0,sizeof(DP)); memset (Max,0,sizeof(Max)); for(i =1; I <= m; i++)///I represents the number of groups, J represents the number of elements{M=-inf;///statistical maximums are required for each set of multiple points for(j = i; J <= N; j + +)///J starts from I because the number of groups is definitely less than the number of elements{Dp[j]= Max (dp[j-1]+A[J], max[j-1]+A[J]);///Dp[j-1]+a[j] stands for a[j] Independent group, MAX[J-1]+A[J] represents A[j] added to the previous groupmax[j-1] = M;///The maximum value is saved first, then the maximum value of this group is calculated, then the next time you use the max array, it becomes the maximum value of the previous group .M =Max (M, Dp[j]); }} printf ("%d\n", M); } return 0;}
HDU Max Sum plus plus (two-dimensional arrays into one-dimensional arrays)