基礎DP+滾動數組---Max Sum Plus Plus HDU - 1024

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. ^_^

InputEach 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.
OutputOutput the maximal summation described above in one line.
Sample Inputhtml

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Outputc++

6
8

Hint數組

Huge input, scanf and dynamic programming is recommended.

解題思路參考自https://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html

本題的大體意思爲給定一個數組,求其分紅m個不相交子段和最大值的問題。spa

設num爲給定數組,n爲數組中的元素總數,dp[i][j]表示前i個數在選取第i個數的前提下分紅j段的最大值,其中1<=j<=i<=n && j<=m,狀態轉移方程爲:code

dp[ i ][ j ] = max(dp[ i-1 ][ j ] + num[ i ],max( dp[ 0 ][ j-1 ]~dp[ i-1 ][ j-1 ]) + num[ i ])htm

乍看一下這個方程挺嚇人的,由於題中n的限定範圍爲1~1,000,000而m得限定範圍沒有給出,m只要稍微大一點就會爆內存。但仔細分析後就會發現 dp[ i ][ j ] 的求解只和 dp[ * ][ j ] 與  dp[ * ][ j-1 ] 有關因此本題只須要兩個一維數組便可搞定狀態轉移。blog

在進行更進一步的分析還會發現其實 max(dp[ 0 ][ j-1 ]~dp[ i-1 ][ j-1 ]) 根本不須要單獨求取。在求取now_Status(保存本次狀態的數組)的過程當中便可對pre_Status(保存前一次狀態的數組)進行同步更新。內存

 。ci

總結dp[ i ][ j ] 表示前i個數在選取第i個數的前提下分紅j段的最大值get

          因爲子段必須是連續的,因此 num[ i ]必須選取的前提下十分重要,不然 dp[ i-1 ][ j ] + num[ i ]不成立

方程解釋:

(1) 當num[i]合併到上一個段時:dp[ i-1 ][ j ] + num[ i ]

(2)當num[i]做爲獨立的一段時(分段,此時上一個狀態應爲 j-1段 ):max( dp[ 0 ][ j-1 ]~dp[ i-1 ][ j-1 ]) + num[ i ]

在這個思路的基礎上(每個狀態的求取只與上一個狀態有關),我將這兩個一維數組換成滾動數組  dp[ i ][2] ,那麼在在每次更新狀態的時候對 j%2 即爲 j 應在位置。

而後求 max( dp[ 0 ][ j-1 ]~dp[ i-1 ][ j-1 ]) 只須要用變量_max不斷地求前綴最大值便可。

須要注意的是當 i 小於 j 時值不存在,dp的值 應置爲-inf。

還有就是   dp[n]是選取num[n]的前提下的值,因此dp[n]不必定是最大值,故須要一個ans來不斷地保存最大值。
 
AC代碼:
 
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn=1e6+2;
#define inf 0x7fffffff
ll dp[maxn][2];        //dp:保存當前狀態和上一次狀態的最大值
ll num[maxn];          
int n,m;

int main()
{
    while(scanf("%d%d",&m,&n)==2){
        dp[0][0]=0;   dp[0][1]=0;  
        for(int i=1;i<=n;i++){
            scanf("%lld",&num[i]);
            dp[i][0]=0;
            dp[i][1]=0;
        }
        ll _max,ans;
        for(int j=1;j<=m;j++){
            _max=-inf;
            ans=-inf;
            dp[j-1][j%2]=-inf;        //注意當i小於j時值不存在,置爲-inf
            for(int i=j;i<=n;i++){
                _max=max(_max,dp[i-1][(j-1)%2]); //計算dp[1][j-1]~~dp[i-1][j-1]的最大值
                dp[i][j%2]=max(dp[i-1][j%2]+num[i],  _max+num[i]);  //合併or分段
                ans=max(ans,dp[i][j%2]);////由於dp[n]是選取num[n]的前提下的值,因此dp[n]不必定是最大值  
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
相關文章
相關標籤/搜索