HDOJ 1024 Max Sum Plus Plus 最大M字段和

来源:互联网 发布:汉诺塔递归算法java 编辑:程序博客网 时间:2024/05/23 01:18

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 32 6 -1 4 -2 3 -2 3
 

Sample Output
68
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