HDU 1024 Max Sum Plus Plus (DP求最大子段和+滚动数组)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36330    Accepted Submission(s): 12928

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.

题意:有一个序列:S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767).给出一个变量m,要求m对i、j使得sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m)最大,同时满足每一对都不交叉。求总和。

题解:dp[ i ][ j ]表示前 j 个数在取 a[ j ] 的情况下分 i 段的最大和。所以dp[ i ][ j ] 可以分为两种情况:1、将第 j 个数看成一个子段则前面 j-1 个数分为 i-1 个子段即dp[ i-1 ][ j-1 ]+a[ j ]。2、将第 j 个数与前面的一些数看成一个子段即dp[ i ][ j-1 ]。

可推出公式dp[ i ][ j ]=max(dp[ i ][ j-1 ],max(dp[ i-1 ][ i-1 ]~~dp[ i-1 ][ j-1 ]))+a[ j ]。

我们结合图表来理解:

表中数据表示dp[ i ][ j ],我们可以发现每一个格子里的数据都是有上一行的最大值与左边的这个数相比较选择一个最大的数据然后加上a[ j ]即公式:dp[ i ][ j ]=max(dp[ i ][ j-1 ],max(dp[ i-1 ][ i-1 ]~~dp[ i-1 ][ j-1 ]))+a[ j ]。

因为若将max(dp[ i-1 ][ i-1 ]~~dp[ i-1 ][ j-1 ])中的所有数据遍历一遍且n最大为1,000,000会超时。所以需要定义一个maxn来找到最大的那个数并且运用滚动数组来实现。

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[1000005];
const int inf=0x3f3f3f3f3f3f;
int dp[2][1000005];
int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        int t=1;//用来滚动数组
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            dp[0][i]=-inf;
        }
        int maxn;
        for(int i=1;i<=m;i++,t=1-t)//t=1-t表示滚动循环
        {
            dp[t][i]=dp[1-t][i-1]+a[i];//求每一次滚动的第一个数据即表中的对角线的值即前n项和
            maxn=dp[1-t][i-1];//一定要在每滚动一次t是初始化maxn
            for(int j=i+1;j<=n;j++)
            {
                maxn=max(maxn,dp[1-t][j-1]);//循环更新max(dp[i-1][i-1]~dp[i-1][j-1])
                dp[t][j]=max(maxn,dp[t][j-1])+a[j];//状态转移获得j处的最大和
            }
        }
        t=1-t;//回到最后一次滚动状态
        maxn=-inf;
        for(int i=m;i<=n;i++)
            maxn=max(maxn,dp[t][i]);//求得n内的最大的m段子段和
        printf("%d\n",maxn);
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_41233888/article/details/81205210