1. 程式人生 > >hdu1024HDU 1024 Max Sum Plus Plus(動態規劃 很詳很詳解)

hdu1024HDU 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): 6725    Accepted Submission(s): 2251


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. 思考了很長很長時間,不過解決後放了一天,今天終於義無反顧的一起來就放部落格了!!

昨天和舍友BR聊到4點左右,看來放假了也得早點回去了(避免落單在宿舍)


言歸正傳,卡了那麼久,DP獨立思考
</pre><pre name="code" class="cpp">#include<iostream>//1000ms卡到984ms ^^ 故意的哈(幾個月以前的記錄,這句廢話不打算刪)
#include<cmath>
#include<string.h>
using namespace std;
int  dp[1222222],alone[1222222],a[1222222];
int main()
{
    int i,j,n,m;
    while(~scanf("%d",&m))
    {
        scanf("%d",&n);
		memset(dp,0,sizeof(dp));
		memset(alone ,0,sizeof(alone));
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        int tmax;         
//★迴圈i內的alone[j]:儲存前j個數(含j)分i段時的最大和★
        for(i=1;i<=m;i++)//★分i段
        {
            tmax=-(1<<30);
			//printf("a[],alo[],dp[]\n");
            for(j=i;j<=n;j++)//從i開始列舉:至少分i段,這裡就直接前i個數i段
            {
                dp[j]=_cpp_max(dp[j-1],alone[j-1])+a[j];
/*★★dp前者表示a[j]與a[j-1]所在的一段合併成一段。al後者表示以a[j]為首開始第 i 段
  ★迴圈i內的alone[j]:儲存前j個數(含j)分i段時的最大和★所以可以有alone[j-1]+a[j]
  alone在同一次的j迴圈總是走不到一起,能走到一起的是下個i的"j迴圈"//*///\
  printf("%2d %2d %2d\n",a[j],alone[j-1],dp[j]);
                if(j>i)alone[j-1]=tmax;
//★迴圈i內的alone[j]:儲存前j個數(含j)分i段時的最大和(所以答案是最後一次迴圈(分n段)時候的tmax)★//\
  alone巧妙的儲存賦值到i層j-1而又不影響當前i-1層的j-1的使用,代表前j-1個分i段最大和.//\
細心點會發現最大隻儲存到alone[n-1],reason:分i段時,列舉用的是分★i-1段層的alone[i-1至n-1](<span style="font-family: Arial, Helvetica, sans-serif;">所以答案是最後一次迴圈時候( 可含a[n])的tmax)</span><span style="font-family: Arial, Helvetica, sans-serif;">//\</span>
比如分n-1段,最後第1次進內迴圈時起點是j=n-1,即前面n-2個數單獨分成n-2段(所以答案是最後一次迴圈(分m段)時候的tmax)
                if(tmax<dp[j])tmax=dp[j];
			//本輪的alone[j-1]儲存此i輪的dp[j]的最大值(即前j-1個數(含j-1)分i段時的最大和★)
            }
			//puts("");puts("");
        }
        printf("%d\n",tmax);
    }
    return 0;
}

/*
帶★(寫完解釋的那天晚上想說,老人都看懂了......現在又+了些解釋--發現不夠精簡!)
雖然表達能力很菜,不過已經講得很清楚了,還不懂的話用下面的樣例去執行觀察吧(m改成6吧)
2 6
-1 4 -2 3 -2 3
取4再區3,-2,3 or 4,-2,3+3=8
*/