HDOJ 1024 Max Sum Plus Plus 最大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 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.
原题链接:  点击打开链接  

思路:动态规划

假设 dp[i][j] 表示前 i 个 元素中 j  段的最大和,且第j段包含 a[i].  则递推公式为

其中, 表示 将 a[i] 加入到 a[i-1] 所在的字段,    则是 从 j-1 到 i-1 中分成 j-1 段的最大值, a[i] 单独作为第j段的情况。

最后的结果为   。

代码如下:

#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;

const int maxn = 1000000+5;


int main() {
    //freopen("input.txt","r",stdin);
    int a[maxn];
    int n,m;
    while(cin>>m>>n) {
        if(m > n) {
            cout<<0<<endl;
            continue;
        }
        for(int i =1 ; i <= n; i++) cin>>a[i];
        int dp[maxn][maxn];
        memset(dp,0,sizeof(dp));
        for(int j = 1; j <= m; j++) {
            dp[j][j] = dp[j-1][j-1] + a[j];
            int maxv = dp[j-1][j-1];
            for(int i = j + 1; i + m - j <= n; i++) {
                maxv = max(maxv,dp[i-1][j-1]);
                dp[i][j] = max(dp[i-1][j],maxv) + a[i];
            }
        }

        int res = INT_MIN;
        for(int i = m; i <= n; i++) {
            if(dp[i][m] > res) res = dp[i][m];
        }
        cout<<res<<endl;
    }
    return 0;
}
上面这种方法会超空间。 我们观察到 求解 dp[i][j] 时, 只用到了 j - 1 段 和 j 段时的值, 所以可以压缩空间。 在计算    , 可以每次记录最大值, 可以压缩时间

代码如下:

#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;

const int maxn = 1000000+5;


int main() {
    //freopen("input.txt","r",stdin);
    int a[maxn];
    int n,m;
    while(cin>>m>>n) {
        if(m > n) {
            cout<<0<<endl;
            continue;
        }
        for(int i =1 ; i <= n; i++) cin>>a[i];
        int dp[maxn][2];
        int p = 0;
        memset(dp,0,sizeof(dp));
        for(int j = 1; j <= m; j++) {
            dp[j][p] = dp[j-1][1-p] + a[j];
            int maxv = dp[j-1][1-p];
            for(int i = j + 1; i + m - j <= n; i++) {
                maxv = max(maxv, dp[i-1][1-p]);
                dp[i][p] = max(dp[i-1][p], maxv) + a[i];
            }
            p = 1 - p;
        }
        p = 1- p;
        int res = INT_MIN;
        for(int i = m; i <= n; i++) {
            if(dp[i][p] > res) res = dp[i][p];
        }
        cout<<res<<endl;
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhengjihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值