zoukankan      html  css  js  c++  java
  • (动态规划)Max Sum Plus Plus--hdu--1024

    http://acm.hdu.edu.cn/showproblem.php?pid=1024

    Max Sum Plus Plus

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


    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
     
    http://www.cnblogs.com/lishuhuakai/archive/2012/10/13/4840323.html  (感觉写的很不错, 反正我是看懂了)
     
     
    这道题在国赛前我就看了, 可是一直不知如何下手, 终于决定一定要把它给A了, 现在的我能沉下心来, 好好看解析, 好好的自己去理解, 果然还是懂了的
     
    重点在递归式上
     

    w[i][j]: 前 j 个数分为 i 段, 第 j 个数必须选;1. 第 j 个数单独为1段;2. 第 j 个数与前面的数连一块。
    w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
    b[i][j]:前 j 个数分为 i 段, 第 j 个数可选可不选; 1.选第 j 个数;2.不选第 j 个数。
    b[i][j] = max(b[i][j-1], w[i][k]);

    /// w[j] 表示 j 个元素取 i 段, a[j] 必须取是的最大值
    w[j] = max(dp[1-t][j-1], w[j-1]) + sum[j]-sum[j-1];
    /// dp[t][j] 表示在a[j]可取可不取这两种情况下取得的最大值
    dp[t][j] = max(dp[t][j-1], w[j]);

     
     
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define N 1000001
    using namespace std;
    
    int sum[N], w[N], dp[2][N];
    
    ///sum[i] 里面存的是前 i 项和
    
    int main()
    {
        int m, n;
    
        while(scanf("%d%d", &m, &n)!=EOF)
        {
            int i, j, x;
    
            sum[0] = 0;
            for(i=1; i<=n; i++)
            {
                scanf("%d", &x);
                sum[i] = sum[i-1] + x;
                dp[0][i] = 0;  ///从前 i 个元素中取 0 段, 最大值为 0
            }
            
            /**
            
            我们先假设a[i]中 存放该序列的第 i 个值,
            w[i][j] 表示前 j 个数分为 i 段, 第 j 个数必须选这种情况下取得的最大值
            b[i][j]表示在前 j 个数中取 i 段 这种情况写取得的最大值
            
            w[i][j]: 前 j 个数分为 i 段, 第 j 个数必须选;1. 第 j 个数单独为1段;2. 第 j 个数与前面的数连一块。
                    w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
            b[i][j]:前 j 个数分为 i 段, 第 j 个数可选可不选; 1.选第 j 个数;2.不选第 j 个数。
                    b[i][j] = max(b[i][j-1], w[i][j]);
            
            **/
            
    
            int t=1;
            for(i=1; i<=m; i++)  /// i表示取 i 段
            {
                for(j=i; j<=n; j++) /// 如果dp[i][j](j<i)是没有意义的
                {
                    if(i==j)
                        dp[t][j] = w[j] = sum[j];
                    else
                    {
                        /// w[j] 表示 j 个元素取 i 段, a[j] 必须取是的最大值
                        w[j] = max(dp[1-t][j-1], w[j-1]) + sum[j]-sum[j-1];
                        /// dp[t][j] 表示在a[j]可取可不取这两种情况下取得的最大值
                        dp[t][j] = max(dp[t][j-1], w[j]);
                    }
                }
                t = 1-t;  ///t在 0 和 1 直间交替变换
                
                /**
                
                为什么要交换呢??? 这是为了要节省空间
                仔细观察递归式
                w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
                b[i][j] = max(b[i][j-1], w[i][j]);
                我们发现,对于取 i 段, w[i][j] 只与 b[i-1][k-1] 和 w[i][k-1] 有关, 与之前的那一些项没有关系
                因此我们的数组可以开小一点, 用更新来覆盖掉前面的值!!!
                
                **/
             }
    
            printf("%d
    ", dp[m%2][n]);
        }
        return 0;
    }
    View Code
     
     
     
     
    勿忘初心
  • 相关阅读:
    js上传文件(图片)限制格式及大小为3M
    position:fixed部分版本的浏览器不支持
    iframe自适应高度的方法
    div左右自适应高度一致
    IE中部分版本的浏览器对Select标签设置innerHTML无效的问题
    在ie10中如何禁用输入框中的小眼睛 与 叉叉 删除按钮
    input输入框默认文字,点击消失
    调用iframe中父页面/子页面中的JavaScript方法
    iframe的一些介绍
    artDialog的一些例子与一些属性的介绍。
  • 原文地址:https://www.cnblogs.com/YY56/p/4919893.html
Copyright © 2011-2022 走看看