HDU1024 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): 42745    Accepted Submission(s): 15453


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 8
Hint
Huge input, scanf and dynamic programming is recommended.

 

解析:

题意:

在n个数中划分不重叠的连续的m个子段,使得子段和最大。


 

dp[i][j]表示当选择第j个数时,划分i组数的最大和。

那么对于任意一个j,我们有两种决策:

  1. 把它跟之前已经划分的组合并。
  2. 它自己独立成新的一组。

我们就可以写出状态转移方程:

dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j]),其中i-1<k<=n

第一个决策dp[i][j-1]+a[j]对应着决策1,第二个决策dp[i-1][k]+a[j]对应着决策2。

那么为什么k的取值是i-1<k<=n呢?

首先,我们必须明确,在划分第i个组时,之前的i-1个组至少有i-1个数,如果此时k再取到i-1或者之前的值的话,就是一定不成立的,毕竟你不能在5个数里面划出来10个不重叠的区间。

但是这样做不仅会MLE,还会TLE。

 

我们就要考虑优化。

首先是滚动数组,因为我们注意到dp数组的当前状态仅仅与之前的一个状态有关,而不需要去考虑更新之前所有状态,所以我们加一个滚动。

但是这样只优化了空间。

 

然后我们就要考虑处理这个k。

我们要明确k的含义:在划分第i组时,我们要从划分i-1组时的状态中找到最优状态进行转移。那我们不妨在每次转移过程中就记录好当前阶段的最优状态,以便下一阶段选取k时之间就可以选取这个最优状态,而不必再扫一遍i-1这个阶段。

所以我们就可以考虑搞一个数组pre[i]记录一下划分第i个组时的最优解。

但是如果让数组pre[j]表示在选取第j个数时之前阶段的最优解,可以优化更多,因为它让pre数组重复利用,dp可以进一步压缩成一维。

所以我们每次选第j个数的时候,就更新一下pre[j-1],把不包括j-1的最大和存起来。注意要在每次状态转移之后在更新当前最大和pre数组!

最终我们得到状态转移方程:

dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]);

参考代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<ctime>
 6 #include<cstdlib>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<map>
11 #define INF 0x3f3f3f3f
12 const int N=1000010;
13 using namespace std;
14 int a[N],dp[N],pre[N];
15 int main()
16 {
17     int n,m;
18     while(scanf("%d%d",&m,&n)!=EOF)
19     {
20         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
21         memset(dp,0,sizeof(dp));
22         memset(pre,0,sizeof(pre));
23         dp[0]=-INF;
24         int ans;
25         for(int i=1;i<=m;i++){
26             ans=-INF;
27             for(int j=i;j<=n;j++){
28                  dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]);
29                  pre[j-1]=ans;
30                  ans=max(ans,dp[j]);
31              }
32         }
33         cout<<ans<<endl;
34     }
35     return 0;
36 }

 

猜你喜欢

转载自www.cnblogs.com/DarkValkyrie/p/11105771.html