http://acm.hdu.edu.cn/showproblem.php?pid=1024
Max Sum Plus Plus
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. ^_^
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
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
8
Hint
Huge input, scanf and dynamic programming is recommended.
题意:将一串数字分成m段子序列,求最大可能达到的总和。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1000005
#define INF -1000000000
int num[N],dp[N],mmax[N];
/*
状态DP[i][j]
前j个数可以分成i组的和的最大值
DP[i][j]=max(dp[i][j-1]+num[j], max( dp[i-1][k] )+num[j]),0<k<j;
dp[i][j-1]是把num[j]加入到前面的一个组,
max(dp[i-1][k])+num[j]是把num[j]重新分在另一个组里面,
而max(dp[i-1][k])是分成i-1个组时候的和的最大值
开一个mmax数组每次都可以保存分成i-1个组的时候使用前j-1个数时候最大值
时间复杂度O(mn)
*/
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n)){
for(int i=;i<=n;i++){
scanf("%d",num+i);
}
memset(dp,,sizeof(dp));
memset(mmax,,sizeof(mmax));
int ans;
for(int i=;i<=m;i++){
ans=INF;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-],mmax[j-])+num[j];
mmax[j-]=ans;
ans=max(dp[j],ans);
}
}
cout<<ans<<endl;
}
return ;
}
2016-06-21