HDOJ-1024 Max Sum Plus Plus (最大M子段和問題)


Max Sum Plus Plus

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


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

 

Author
JGShining(極光炫影)
 

【問題描述】----最大M子段和問題
給定由 n個整數(可能為負整數)組成的序列a1,a2,a3,……,an,以及一個正整數 m,要求確定序列 a1,a2,a3,……,an的 m個不相交子段,
使這m個子段的總和達到最大,求出最大和。

題解:轉自http://www.cnblogs.com/peng-come-on/archive/2012/01/15/2322715.html
動態規划的思想。
1.基本思路:
  首先,定義數組num[n],dp[m][n].
  num[n]用來存儲n個整數組成的序列.
  dp[i][j]用來表示由前 j項得到的含i個字段的最大值,且最后一個字段以num[j]項結尾。仔細想想,我們可以知道:
  dp[i][j]=max(dp[i][j-1]+num[j],dp(i-1,t)+num[j])   其中i-1<=t<=j-1.
  (因為必須是以 num[j] 結尾的,所以num[j]一定屬於最后一個子段,即要么自己獨立成一個子段,要么與前邊以num[j-1]結尾的子段聯合)
  所求的最后結果為 max( dp[m][j] ) 其中1<=j<=n.
  但是,我們會發現,當n非常大時,這個算法的時間復雜度和空間復雜度是非常高的,時間復雜度近似為O(m*n^2),
  空間復雜度近似為O(m*n).因此,我們需要優化算法來降低時間復雜度和空間復雜度.
2.優化算法:
  (1)節省時間
  由基本思路,我們可以知道,dp[i][j]=max(dp[i][j-1]+num[j],dp(i-1,t)+num[j]),其中i-1<=t<=j-1.我們只要找到dp[i][j-1]
  和dp[i-1][t]的最大值加上num[j]即為dp[i][j].所以,定義一個數組pre_max[n],用pre_max[j-1]來表示求解dp[i][j]時dp[i-1][t]
  的最大值,則dp[i][j]=max(pre_max[j-1],dp[i][j-1])+num[j].
  特別注意,pre_max[n]這個位置的存儲空間是始終用不到的,因此可以用來存儲其他數值,在接下來會用到。
  在求解dp[i][j]的同時,我們可以計算出dp[i][t];i<=t<=j的最大值,這個最大值在計算dp[i+1][j+1]的時候需要作為pre_max[j]的
  形式被使用,我們先把它存在pre_max[n]中。
  你可能會問:為什么不把它直接放在pre_max[j]中呢?因為你接下來需要計算dp[i][j+1]的值,需要用到pre_max[j]中原來的值,
  如果你把它存在這里,就會覆蓋掉計算dp[i][j+1]所需要的那個值。所以,先把它放在pre_max[n]中。
  當我們計算完dp[i][j+1]之后,就會發現pre_max[j]中的值已經沒有用處了,我們可以把它更新為計算dp[i+1][j+1]所需要的那個值,
  即之前放在pre_max[n]中的那個值,即執行pre_max[j]=pre_max[n].
  這樣我們就節省了計算最大值時付出的時間代價。
  (2)節省空間
  通過時間的節省,我們突然間發現程序執行結束后pre_max[n]的值即為最后的結果,pre_max[n]數組才是我們希望求解的,
  dp[m][n]這個龐大的數組已經不是那么重要了,因此,我們現在用整型數tmp來代替dp[m][n],用來臨時存儲dp[i][j]的值,
  作為求解pre_max[n]的中介。
  這樣就節省了dp[i][j]占用的極大的空間.

代碼一:

 1 #include <cstdio>
 2 #include <iostream>
 3 const int MAX = 1000005;
 4 
 5 using namespace std;
 6 
 7 int num[MAX], pre_max[MAX]; 
 8 
 9 inline int max(int a, int b)
10 {
11     return a > b ? a : b;
12 }
13 
14 int DP(int n, int m)
15 {
16     for(int i = 1; i <= m; ++i)
17     {
18         /*****初始化*****/ 
19         int tmp = 0;
20         for(int k = 1; k <= i; ++k)
21             tmp += num[k];
22         pre_max[n] = tmp;
23         
24         for(int j = i+1; j <= n; ++j)
25         {
26             tmp = max(pre_max[j-1], tmp) + num[j];
27             pre_max[j-1] = pre_max[n];
28             pre_max[n] = max(pre_max[n], tmp);         
29         }
30     }
31     return pre_max[n];
32 }
33 
34 int main()
35 {
36     int n, m;
37     while(~scanf("%d%d", &m, &n))
38     {
39         for(int i = 1; i <= n; ++i)
40         {
41             scanf("%d", &num[i]);
42             pre_max[i] = 0;   
43         }
44         printf("%d\n", DP(n, m));
45     }
46     return 0;
47 } 

 代碼二:(討論區粘的)

 1 #include<iostream>
 2 #include<climits>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 using namespace std;
 7 int max(int *a,int m,int n)
 8 {
 9     int *c;
10     int *p;
11     int max, i, j;
12     c=new int[n+1];
13     p=new int[n+1];
14     for(i=0; i<n+1; i++)
15         p[i]=0;
16     c[0]=0;
17     for(i=1; i<=m; ++i)
18     {
19         max=INT_MIN;
20         for(j = i; j <= n; ++j)
21         {
22             if(c[j-1]< p[j-1])
23                 c[j]= p[j-1]+a[j-1];
24             else
25                 c[j]=c[j-1]+a[j-1];
26             p[j-1]=max;
27             if(max<c[j])
28                 max=c[j];
29         }
30         p[j-1]=max;
31     }
32     delete []p;
33     delete []c;
34     return max;
35 }
36 int main()
37 {
38     int n,m,i,*d;
39     while(cin>>m>>n)
40     {
41         d=new int[n];
42         for(i=0;i<n;++i)
43             cin>>d[i];
44         cout<<max(d, m, n)<<endl;
45         delete [] d;
46     }
47     return 0;
48 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM