Max Sum plus plus
Time limit:1 Sec Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=1024
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 the integers m and n, followed by n integers S
1, S
2, S
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 3
2 6-1 4-2) 3-2 3
Sample OUTPUT6
8
HINT
Test Instructions
Give you the number of n, let you choose a continuous m segment, let you get maximum value
Exercises
Simply think, dp[i][j] means, the number of first J, I select the maximum value of the I segment, then the transfer equation, Dp[i][j]=max (dp[i][j-1]+a[j],dp[i-1][k]+a[j])
Then scroll through the array to optimize
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 1000005#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;} while (x>0) ch[++num]=x%10,x/=10; while (Num) Putchar (ch[num--]+48); Puts ("");}*///**************************************************************************************InlineintRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;} while(x>0) ch[++num]=x%Ten, x/=Ten; while(Num) Putchar (ch[num--]+ -); Puts ("");}intdp[maxn+Ten];intmmax[maxn+Ten];inta[maxn+Ten];intMain () {intn,m; intI,j,mmmax; while(SCANF ("%d%d", &m,&n)! =EOF) { for(i=1; i<=n;i++) {scanf ("%d",&A[i]); Mmax[i]=0; Dp[i]=0; } dp[0]=0; mmax[0]=0; for(i=1; i<=m;i++) {Mmmax=-inf; for(j=i;j<=n;j++) {Dp[j]=max (dp[j-1]+a[j],mmax[j-1]+A[j]); Mmax[j-1]=Mmmax; Mmmax=Max (mmmax,dp[j]); }} printf ("%d\n", Mmmax); } return 0;}
HDU Max Sum plus plus DP