hdu 1024 Max Sum Plus Plus

来源:互联网 发布:淘宝店铺资质是什么 编辑:程序博客网 时间:2024/05/17 22:57

Max Sum Plus Plus

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


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
6

8

DP练习,还是不太理解,下边赋值大神的解释2015,5,30

#include<stdio.h>#include<string.h>#define max(a,b) (a > b ? a:b)#define M 1000000+10#define inf 0x3f3f3f3fint now[M],pre[M],a[M];/*now[j],表示以第j个元素为结尾的i个子段的最大和,必须包含a[j]。pre[j],表示前j个元素i个子段的最大和,不一定包含a[j]。dp[i][j],表示前j个元素i个子段的最大和,包含a[j]原始状态转移方程:dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j]) (i-1<=k<=j-1)第1种情况是直接将第j个元素加在第i个子段之后,第2种情况是将第j个元素单独作为一个子段,那么前面必须是i-1个子段*/int main(){int n,m,i,j,mmax;while(~scanf("%d%d",&m,&n)){memset(now,0,sizeof(now));memset(pre,0,sizeof(pre));for(i=1;i<=n;i++) scanf("%d",&a[i]);for(i=1;i<=m;i++){mmax=-inf;for(j=i;j<=n;j++){now[j]=max(now[j-1]+a[j],pre[j-1]+a[j]);//now[j]有两种来源,一种是直接在第i个子段之后添加a[j]            //一种是是a[j]单独成为1个子段            pre[j-1]=mmax;//更新pre使得pre是前j-1个中最大子段和            if(now[j]>mmax) mmax=now[j];}}printf("%d\n",mmax);}return 0;} 


0 0