hdu1024 Max Sum Plus Plus
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21752 Accepted Submission(s): 7282
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. ^_^
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.
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.
这题m的数据范围不知道,是个坑点,看了别人的博客知道只有n*m的算法才能过= =.
刚开始我的思路是设dp[i][j]表示前i个数取j段且第j段包括第i个数的最大和,考虑最后一个数,这个数可以自成一段或者和之前的数组成一段,得到dp[i][j]=max(dp[k][j]+sum[k'][i]).
但是因为不用每个数都选,和区间dp的做法不同,所以这种方法k要枚举,k'也要枚举,会超时,所以这思路不行。
看了别人的思路,发现可以有另一种dp方法,状态表示和前面一样,就是考虑第i个数是延续前一段还是另起一段,那么 dp[i][j]=max(dp[i-1][j]+num[i],dp[k][j-1]+num[i]) j-1<=k<=i-1
另外这题因为m的范围不知道,所以要用滚动数组。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000000000000
#define pi acos(-1.0)
#define maxn 1000050
ll num[maxn],sum[maxn];
ll dp[maxn][2];
int main()
{
ll n,m,i,j,temp,ans;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%lld",&num[i]);
sum[i]=sum[i-1]+num[i];
}
ans=-inf;
dp[0][1]=-inf;
for(i=1;i<=n;i++){
dp[i][1]=max(num[i],dp[i-1][1]+num[i] );
ans=max(ans,dp[i][1]);
}
if(m==1){
printf("%lld\n",ans);continue;
}
int pre=1;
for(j=2;j<=m;j++){
ans=-inf;
dp[j][1-pre]=sum[j];
temp=max(sum[j-1],dp[j][pre]);
ans=max(ans,dp[j][1-pre]);
for(i=j+1;i<=n;i++){
dp[i][1-pre]=max(dp[i-1][1-pre],temp)+num[i]; //temp表示dp[k][j-1],每次循环就把i的情况添加进去
temp=max(temp,dp[i][pre]);
ans=max(ans,dp[i][1-pre]);
}
pre=1-pre;
}
printf("%lld\n",ans);
}
return 0;
}