Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16803 Accepted Submission(s): 5525
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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j xor i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^
Given a consecutive number sequence S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j xor i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S
1, S
2, S
3 ... S
n.
Process to the end of file.
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 8HintHuge input, scanf and dynamic programming is recommended.
Author
JGShining(极光炫影)
解题思路:求n个数的m个不相交子区间的和的最大值,令dp[i][j]表示以第i个数为结尾的j个区间的最大值,
dp[i][j]=max(dp[i-1][j]+arr[i],max(dp[k][j-1])+arr[i],0<k<i);
注意到dp[i][j]只与dp[i-1][j]和dp[k][j-1]有关,采用滚动数组优化
同时dp[k][j-1]可以在之前的0---i-1中计算出来用数组保存下
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 1000005
#define Maxm 1000005
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define make_pair MP
#define LL long long
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int arr[Maxn];
int dp[Maxn];
int dpp[Maxn];
int read()
{
int res=0;
bool flag=true;
while(1)
{
char ch=getchar();
if(ch>='0'&&ch<='9')
res=res*10+ch-'0';
else if(ch=='-')
{
flag=false;
continue;
}
else
break;
}
return flag?res:-res;
}
int main()
{
int n,m;
int ans;
//re;wr;
while(~scanf("%d%d",&m,&n))
{
getchar();
for(int i=0;i<=n;i++)
arr[i]=dp[i]=dpp[i]=0;
for(int i=1;i<=n;i++)
arr[i]=read();
for(int j=1;j<=m;j++)
{
ans=-inf;
for(int i=j;i<=n;i++)
//下标要从j开始,这样才能保证滚动数组是正确的
{
dp[i]=max(dpp[i-1],dp[i-1])+arr[i];//dp[i][j]=max(dp[i-1][j]+arr[i],max(dp[k][j-1])+arr[i],0<k<i)
dpp[i-1]=ans;//dpp存储max(dp[k][j-1]),0<k<i
ans=max(ans,dp[i]);//ans表示以i为结尾的选择了j段的最大值
}
}
printf("%d\n",ans);
}
return 0;
}