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 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
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 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
题意:给一个含有N个数字的序列,找出M个不相交的子段,使得子段和最大。
思路:设dp[i][j]为前i个数中选出j个子段能得到的最大和,且第j段以a[i]结尾。
若第j段仅由a[i]组成,则dp[i][j]=max(dp[k][j-1])+a[j]。(j-1 <= k <= i-1)
若第j段仅由a[i]与a[i-1]等组成,则dp[i][j]=dp[i-1][j]+a[i]。
首先M未知,二维数组存不了。但是我们可以发现,j状态总是从j-1状态推来的,所以利用滚动数组,可以将dp变为一维数组。
此外,如果用三重循环一定会超时,所以我们需要预处理出max(dp[k][j-1])的部分,设数组imax[i-1]表示max(dp[k][j-1]),j-1 <= k <= i-1。
由于如果我们dp[i]更新后立即更新imax[i]的话,此时imax[i]则表示max(dp[k][j]),当我们枚举到i+1时,就用的是max(dp[k][j])+a[j],而不是max(dp[k][j-1])+a[j]。这样是错误的。所以我们要先记录下imax[i]的值,当枚举当i+1时再更新imax[i]。
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
int n,m;
int a[1000005];
int dp[1000005];
int imax[1000005];
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(imax,0,sizeof(imax));
memset(dp,0,sizeof(dp));
for(int j=1;j<=m;j++)
{
int mmax=-0x3f3f3f3f;
for(int i=j;i<=n;i++)
{
dp[i]=max(dp[i-1]+a[i],imax[i-1]+a[i]);
imax[i-1]=mmax;
mmax=max(mmax,dp[i]);
}
}
int ans=-0x3f3f3f3f;
for(int i=m;i<=n;i++)
{
ans=max(dp[i],ans);
}
cout<<ans<<endl;
}
return 0;
}