zoukankan      html  css  js  c++  java
  • HDU 1024 max sum plus

    A - Max Sum Plus Plus
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Appoint description:

    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. 
             

    题意是输入m,n。

    m为你要求的子段个数,n为数据个数。


    由于是很早的题型了,但是理解起来还是很是无力。

    并于是用了三天来搞懂此类问题。发现网上大多代码无思路整个过程。


    就大致讲解一下DP的整个思路。

    我们可以很清楚地得,这是一个顺序DP,由ix<=iy可得。

    那它在当前位置或者称为状态,又能做出几个决策才能保证到目前状态是最大的呢?

    当然是逐步取其上一状态的最大值。

    综上所述就可以得此模糊状态转移方程:dp[i][j] =MAX(dp[?][?],dp[??][??]) (或者是三个或者更多)。


    下面我们来找它转移方程的本质:

    设dp[i][j] 为前j个数字组成i段的最大和。(前提是i<=j ,这是显而易见的,当小于的时候不能组成i段)

    所要达到的状态 :dp[i][j].

    需要的条件:1、前j-1个数,组成i段的最大和 ;2、前j-1个数,组成的i-1段的最大和。

    解释:由dp[i-1][j-1]到dp[i][j],指的是当前第j个数单独为一段,dp[i][j-1]->dp[i][j],第j个数接在第i段后面。

    继续深入:我们是要求dp[i-1][j-1],dp[i][j- 1]的最大值。那么i段就是在当前这个循环更新i->n状态的,所以dp[i][j-1]时刻在更新,反之,当前dp[i-1][j-1]的状态没 有得到更新,那么我们就要加入这个j号元素进入时的更新保证dp[i-1][j-1]是当前可满足状态中最大的。

    完全的动态规划转移方程dp[i][j]=MAX(dp[i][j-1],dp[i-1][k]) 其中i<=k<=j。

    回头再看:n的最大值为100W,二维数组出来就需要巨额的空间。但是想象,当前需要更新的状态是不是就与当前上一状态和上一层的状态有关。

    最终解决办法:采用滚动数组(或者两个一维数组)。

    下面有两种转移方程的格式,其中也有一点不同,具体代码如下:

    #include<cstdio>
    #include<cstring>
    #define MIN -(1<<30)
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define M 1000001
    int dp[2][M],num[M],n,m,max;
    
    int main(){
    
        while(~scanf("%d %d",&m,&n)){
            int i=1,j,t=1;
    
            while(i<=n)
                scanf("%d",&num[i]),dp[0][i]=dp[1][i]=0,i++;
            
            for(i=1;i<=m;i++,t=!t){
    
                dp[t][i]=dp[!t][i-1]+num[i];
                dp[!t][i]=Max(dp[!t][i],dp[!t][i-1]);
    
                for(j=i+1;j<=n-m+i;j++){
    
                    dp[t][j]=Max(dp[!t][j-1],dp[t][j-1])+num[j];
                    dp[!t][j]=Max(dp[!t][j],dp[!t][j-1]);
    
                }
            }
            max=MIN;
            for(i=m;i<=n;i++)
                max=Max(dp[m&1][i],max);
    
            printf("%d
    ",max);
    
        }
        return 0;
    }
     

    第二种方法只有一出有所不通,就是采用两个一维数组进行更新:

    dp[],t[],但是更值得注意的是,只有dp[j]是当前的状态,而t[k] (i<=k<=j)是当前状态的原状态,所以t[j-1]实际上是代替了方法中的dp[i][j-1],

    换言之。就是t[]中 i->j 是接在dp[j]后面的。而dp[i->j],是上一层的状态。这也是为什么t[j-1]要用max来保存,它表示的是当前层的最大值。

    不知道有没有讲清楚。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define maxn 1000003
    #define INF 100000000
    int dp[maxn],a[maxn],pre[maxn];
    int main(){
        int n, m, i, j, maxx;
        while(~scanf("%d%d",&m,&n)){
            for(i=1;i<=n;i++)
                scanf("%d",&a[i]);
            dp[0]=0;
            memset(pre,0,sizeof(pre));
            for(i=1;i<=m;i++){
                maxx =-INF;
                for(j=i;j<=n;j++){//对于每个i,随着j的增大,maxx越滚越大
                    dp[j]=max(dp[j-1],pre[j-1])+a[j];
                    pre[j-1]=maxx;//把前一轮的最大值赋给pre;
                    printf("dp[%d]=%d pre[%d]=%d
    ",j,dp[j],j-1,pre[j-1]);
                    maxx=max(maxx,dp[j]);
                }
                puts("");
            }
            printf("%d
    ",maxx);//最后一轮的最大值就是答案。
                                //因为上一个循环中对于每个i,随着j的增大,maxx越滚越大
        }                       //而且pre也是越滚越大的。
        return 0;
    }
    //给了一组数据,不理解就把所有DP打出来,自己手动模拟一遍,这样好理解多了
    
    /*
    
    4 6
    2 -4 5 6 -8 10
    
    */

     

  • 相关阅读:
    HDU 1016 Prime Ring Problem
    POJ 1724 ROADS(bfs最短路)
    HDU 1033 Edge
    IE 兼容模式
    HDU 1263 水果
    数据结构之图详解
    继续过中等难度.0309
    排序的稳定性
    Java+7入门经典
    哈希链表及其变种
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/5356806.html
Copyright © 2011-2022 走看看