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 input1 3 1 2 32 6-1 4-2 3-2 3
Sample output68
HintHuge input, scanf and dynamic programming is recommended.
Authorjgshining (Aurora shadow)
Recommendwe have carefully selected several similar problems for you: 1074 1025 1081 1080 general purpose: to reduce the complexity, I would like to ask you how to divide the number of M into N consecutive parts, the maximum sum is the definition of DP [I] [J], which indicates that the current number is I and has been divided into the direction where J blocks can be transferred. 1. the number of blocks does not change. 2. the number of blocks moves the equation DP [I] [J] = max (DP [I-1] [J] + A [I], max (DP [1] [J-1]... DP [I-1] [J-1]) + A [I]) bing God code good and bad .. Define an array record the largest value in the front J-1 of the previous block so the final answer is this max2 (the largest value between 1 and J when divided into N groups)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000010;const int inf = 0x3f3f3f3f;int dp[maxn];int a[maxn],max1[maxn];int main(){ int n,m; int max2; while(~scanf("%d%d",&n,&m)){ for(int i = 1; i <= m ;i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); memset(max1,0,sizeof(max1)); for(int i = 1; i <= n; i++){ max2 = -inf; for(int j = i; j <= m; j++){ dp[j] = max(dp[j-1]+a[j],max1[j-1]+a[j]); max1[j-1] = max2; max2 = max(max2,dp[j]); } } printf("%d\n",max2); } return 0;}
HDU1024--DP -- Max sum plus