Max Sum plus PlusTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 20107 Accepted Submission (s): 6638
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, S
2, S
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)
Recommend
/* Test Instructions: The number of n is divided into M-sets to find the maximum value of the set: Dp[i][j] represents the maximum number of J-sets for the first I, so the transfer equation Dp[i][j]=max (dp[k][j-1],dp[i-1][j]) +a[i] J-1=<k<=i here J can scroll to save space * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) # Define Debug printf ("%d\n", bug++) #define EPS 1e-8typedef __int64 ll;using namespace std; #define INF 0x3f3f3f3f#define N 1 000005ll dp[n][2];int bug;int m,n;int a[n];int main () {int i,j; Freopen ("H:/in.txt", "R", stdin); while (~SCANF ("%d%d", &m,&n)) {bug=0; for (i=1;i<=n;i++) scanf ("%d", &a[i]); for (i=0;i<=n;i++) dp[i][0]=dp[i][1]=-1111111111111; dp[0][0]=dp[0][1]=0; int cur=0; for (i=1;i<=m;i++) {dp[i][cur]=dp[i-1][cur^1]+A[i]; ll Ma=dp[i-1][cur^1]; The first i-1 number is combined into the maximum value of the I-1 collection for (j=i+1;j<=n;j++) {Ma=max (DP[J-1][CUR^1],MA); Dp[j][cur]=max (Ma,dp[j-1][cur]) +a[j]; The first j-1 number is combined into the maximum value of the i-1 set before the j-1 is combined into an I set plus this number} cur^=1; } ll ans=-1111111111111; Cur^=1; for (i=m;i<=n;i++) if (Dp[i][cur]>ans) ans=dp[i][cur]; printf ("%i64d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU Max Sum plus Plus (DP)