HDU 1024 Max Sum Plus Plus(最大M段和)

Max Sum Plus Plus

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

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 x or 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.
 

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.
 

Author
JGShining(极光炫影)

 
思路:
dp[i][j]表示分成i段前j个元素的最大和,同时这个最大和是由a[j]结尾的。
状态为i、j,值为dp[i][j]。
若a[j]与前面一段直接相连成为共同的一段,那么前面应该已经有了i段:
dp[i][j] = {dp[i][j-1] + a[j]}
若a[j]单独成一段,那么前面应该是已经有了i-1段:
dp[i][j] = {dp[i-1][k] + a[j]} (i-1<=k<j)
所以状态转移方程为
dp[i][j] = {max(dp[i][j-1], dp[i-1][k]) + a[j]} (i-1<=j<k)

有了状态转移方程,剩下的就是优化了,把状态转移表画出来。
首先对一些特殊情况做一些处理,定义出i,j的边界条件:
    1、第i段可以以任何一个数字结尾;
    2、某段以a[j]结尾的时候,表示前j个元素以j结尾的最大和,也就是说,最多只能分成j段(现在一共只有j个元素,最多的情况下就是每个数字单独成一段,那么刚好是j段),所以无论在什么情况下总有i<=j。

这里以题目中的 -1 4 -2 3 -2 3为例,状态转移表填写过程如下:


填写对角线元素的方法是,dp[i][j] = {dp[i-1][j-1] + a[j]} (i=j)
然后根据状态转移方程接着填


注意到,这里填每一行的时候,其状态仅与上一行有关。也就是说,仅仅依靠第i-1行就能够得到完整的第i行,同样,仅仅根据第i行就能填出i+1行,可以用滚动数组来优化空间,下面代码就用一个dp[2][M]大小的数组来完成整个表的填写。
 
#include <iostream>
#include <cstring>
#include <cstdio>
#define M 1000010
#define INF 9999999

using namespace std;
int a[M];
int dp[2][M];

int Max(int a, int b)
{
    return a>b?a:b;
}

int main()
{
    int m, n;
    while (scanf("%d %d", &m, &n)!=EOF)
    {
        for (int i=1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            dp[0][i] = dp[1][i] = 0;
        }
        
        dp[0][0] = dp[1][0] = a[0] = 0;

        int t = 1;
        for (int i=1; i<=m; i++)
        {
            dp[t][i] = dp[1-t][i-1] + a[i];
            int maxn = dp[1-t][i-1];
            for (int j=i+1; j<=n-m+i; j++)
            {
                maxn = Max(maxn, dp[1-t][j-1]);
                dp[t][j] = Max(maxn, dp[t][j-1]) + a[j];
            }
            t = 1-t;
        } 
        t = 1-t;

        int ans = -1*INF;
        for (int i=m; i<=n; i++)
        {
            if (dp[t][i]>ans)
                ans = dp[t][i];
        }    
        
        cout << ans << endl;
    }    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值