• HDU 1024 Max Sum Plus Plus


    Max Sum Plus Plus

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


    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.
     
    第一次做到滚动数组DP求解,简单来说,考虑到滚动数组的DP是在于如果开成二维数组会过大的长宽,比方说这个题目,如果以dp[i][j]表示前j个数分成i段的话,就有n * m的大小,再有我们发现,每一行的状态都是由上面那一行的状态所决定,不存在跨多行的问题。

    对于这个题目,也是不太会做,看了半天题解。首先写出动态转移方程:

    dp[i][j]=max(dp[i][j-1]+num[j],dp(i-1,t)+num[j]) 其中-- 未完整表达式 i - 1 <= t <= j - 1

    这个比较好理解,就是将j个数分成i份,其中无非分成两种情况,第一种情况是前面与当前的连在一起,第二种情况即两段分开。我们只要找到dp[i][j - 1]和dp[i - 1][t]中的较大的一个加上当前j位置的数,就可以得到dp[i][j],所以我们需要一个数组pre_max[N],用pre_max[j - 1]来代表dp[i - 1][t]的最大值。我们将dp[i][t]的值一直存储,就存储在n的位置。然后再每次更新完当前的pre_max以后更新pre_max[n]位置的值。、

    具体参考:http://www.cnblogs.com/dongsheng/archive/2013/05/28/3104629.html

    代码如下:

    /*************************************************************************
    	> File Name: Max_Sum_Plus_Plus.cpp
    	> Author: Zhanghaoran
    	> Mail: chilumanxi@xiyoulinux.org
    	> Created Time: Thu 22 Oct 2015 06:14:46 PM CST
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    const int N = 1000010;
    int n, m;
    int a[N];
    int pre_max[N];
    
    int dp(int m, int n){
        for(int i = 1; i <= m; i ++){
            int temp = 0;
            for(int t = 1; t <= i; t ++)
                temp += a[t];
            pre_max[n] = temp;
    
            for(int j = i + 1; j <= n; j ++){
                temp = max(pre_max[j - 1], temp) + a[j];
                pre_max[j - 1] = pre_max[n];
                pre_max[n] = max(temp, pre_max[n]);
            }
        }
    
        return pre_max[n];
    }
    
    int main(void){
        while(~scanf("%d %d", &m, &n)){
            memset(pre_max, 0, sizeof(pre_max));
            for(int i = 1; i <= n; i ++){
               scanf("%d", &a[i]);
            }
            cout << dp(m, n) << endl;
        }
        return 0;
    }




  • 相关阅读:
    转:android WebView 文字 、图片分开加载
    js获取网页高度
    [转]URLPath匹配原则
    Java课程设计俄罗斯方块
    Three Little Habits to Find Focus
    ubuntu 12.04 无法联网的问题
    [转]时间去了哪里
    matlab 用plot在图像上面画图
    深入理解ES6临时死区(Temporal Dead Zone)
    sql 连接超时问题
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136079.html
Copyright © 2020-2023  润新知