HDU 1024 Max Sum Plus Plus[dp](最大m子段和)


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个互不相交的子段,使得他们的和最大。输出最大和。


取某一个数有两种方式:要么并入上一个数所属的段,要么作为一个新段的开头(此时上一个数可不要)。

d[i][j]表示取了第i个数后,前i个数构成j个段的最大和。(默认约束j<=i)

d[i][j] = max { d[i-1][j],  d[k][j-1] } + a[i]  , 其中,j-1 <= k <= i-1

优化1:方程中除了i和j有循环外,k也有一个循环。这个循环是用来查找到前i-1个数构成j-1个子段的最大和,既然如此我们直接用一个数组记下来就好了。这样还有一个好处,就是我们可以使用滚动数组来进行递推。

优化2:我们最后一步要递推的是d[n][m],这个状态由哪些状态转移过来呢?

只由d[m-1 ... n-1][m-1]和d[n-1][m](如果这些状态合法的话)。那么我们就完全不需要管d[n-1][m-2]是什么东西(我们要求前n个数的m子段和,那么知道前n-1个数的m-2子段和是没用的)。既然d[n-1][m-2 ... 1]是不需要的状态,那么d[n-2][m-3 ... 1]也都是不需要的状态,对j采用逆向递推,就可以剪枝了。

注意这个dp方程是选了第i个数的情况。第n个数可选可不选,前面的也是如此,因此要在递推过程中将最大的答案记录下来。

#include<iostream>
#include<cassert>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<iterator>
#include<cstdlib>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define debug(x) cout<<"debug "<<x<<endl;
#define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
#define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
#define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
#define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
#define clr(c, x) memset(c, x, sizeof(c) )
typedef long long int64;
const int64 INF = 1e15;
const double eps = 1e-8;


//*****************************************************
const int maxn = 1e6+10;
int64 *pre,*cur;
int64 d[2][maxn];
int64 a[maxn];
int64 mx[maxn];  //优化1 要用的数组

int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n) == 2){
        rep(i,1,n)scanf("%I64d",a+i);
        if(n==1)
        {
            printf("%I64d\n",a[1]);
            continue;
        }

        int64 ans = -INF;
        pre = d[0];
        cur = d[1];

        mx[1] = pre[1] = a[1];
        mx[0] = pre[0] = 0;

        rep(i,2,n)
        {
            mx[i] = cur[i] = pre[i-1]+a[i];
            dep(j,min(m,i-1),1)
            {
                if(i-j>n-m)break;   //优化2
                cur[j] = max(pre[j],mx[j-1])+a[i];
                mx[j] = max(mx[j], cur[j]);
            }
            if(i>=m) ans = max(ans,cur[m]);
            swap(cur,pre);
        }

        printf("%I64d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-12-03 23:43  DSChan  阅读(186)  评论(0编辑  收藏  举报