hdu 1024 Max Sum Plus Plus (DP)

Max Sum Plus Plus

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


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 ≤ jxor 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 3
2 6 -1 4 -2 3 -2 3
 

 

Sample Output
6
8
 
Hint
Huge input, scanf and dynamic programming is recommended.
 

 

Author
JGShining(极光炫影)
 

 

Recommend
We have carefully selected several similar problems for you:  1080 1078 1158 1227 1224 
 

 

 1 //609MS    8460K    692 B    C++
 2 /*
 3 
 4     求最大m段不重叠子序列和。
 5     
 6     DP:
 7     状态转移方程:
 8         dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) 0<k<j
 9         dp[i][j]表示i段时遍历到j并使用a[j]时最大值
10         
11     使用滚动数组 maxn[] 记录 max(dp[i-1][k]) 
12 
13 */
14 #include<stdio.h>
15 #include<string.h>
16 #define N 1000005
17 #define inf 0x7fffffff
18 int dp[N],maxn[N],a[N];
19 inline int Max(int x,int y)
20 {
21     return x>y?x:y;
22 }
23 int main(void)
24 {
25     int n,m;
26     while(scanf("%d%d",&m,&n)!=EOF)
27     {
28         int tmaxn;
29         memset(dp,0,sizeof(dp));
30         memset(maxn,0,sizeof(maxn));
31         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
32         for(int i=1;i<=m;i++){
33             tmaxn=-inf;
34             for(int j=i;j<=n;j++){
35                 dp[j]=Max(dp[j-1],maxn[j-1])+a[j];
36                 maxn[j-1]=tmaxn;
37                 tmaxn=Max(tmaxn,dp[j]);        
38             }
39         }
40         printf("%d\n",tmaxn);
41     }
42     return 0;
43 }

 

posted @ 2014-05-22 08:30  heaventouch  阅读(242)  评论(0编辑  收藏  举报