HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)

来源:互联网 发布:钣金外壳 软件 编辑:程序博客网 时间:2024/06/05 15:17

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

 

Author
JGShining(极光炫影)
 

【问题描述】----最大M子段和问题
给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……,an,以及一个正整数 m,要求确定序列 a1,a2,a3,……,an的 m个不相交子段,
使这m个子段的总和达到最大,求出最大和。

动态规划的思想。
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             else25                 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 }
复制代码
0 0
原创粉丝点击