HDU 1024 Max Sum Plus Plus

来源:互联网 发布:淘宝快递模板怎么设置 编辑:程序博客网 时间:2024/06/05 05:17

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21363    Accepted Submission(s): 7144


Problem Description
Now 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 S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is 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(ix, jx)(1 ≤ x ≤ m) instead. ^_^
 

Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.
 

Output
Output the maximal summation described above in one line.
 

Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
 

Sample Output
68
Hint
Huge input, scanf and dynamic programming is recommended.
求区间最大m个子段和dp[i][j]表示有i段的j个数字的最大和因为每次求解只和dp[i][j]和dp[i][j-1]有关可以压缩
状态转移方程dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j])//0<k<j,且dp[i-1][k]是在i-1段中最大的值我们用sum数组每次保存
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈#include <map>#include <set>#include <queue>#include <cmath>#include <stack>#include <cctype>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define rds(x) scanf("%s",x)#define rdc(x) scanf("%c",&x)#define ll long long int#define maxn 1000010#define mod 1000000007#define INF 0x3f3f3f3f //int 最大值#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define MT(x,i) memset(x,i,sizeof(x))#define PI  acos(-1.0)#define E  exp(1)using namespace std;int dp[maxn],a[maxn],n,m,maxx,sum[maxn];int main(){    while(rd(m)!=EOF){        rd(n);FOR(i,1,n)rd(a[i]);        MT(dp,0);MT(sum,0);        FOR(i,1,m){            maxx=-INF;            FOR(j,i,n){                dp[j]=max(dp[j-1]+a[j],sum[j-1]+a[j]);                sum[j-1]=maxx;                maxx=max(maxx,dp[j]);            }        }        printf("%d\n",maxx);    }    return 0;}/*1 3 1 2 32 6 -1 4 -2 3 -2 3*/


0 0
原创粉丝点击