Max Sum Plus Plus(m个子段的最大和)

来源:互联网 发布:yy网络女主播不雅视频 编辑:程序博客网 时间:2024/05/16 10:32


Max Sum Plus Plus点击打开链接

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


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 32 6 -1 4 -2 3 -2 3
 

Sample Output
68
Hint
Huge input, scanf and dynamic programming is recommended.

对于初学者有思路解决这道题我感觉就很不错了,结果还是你想的那样,虽然每提交一次后,都能有所改进,乐儿,在第三次提交前,我发现自己忽视了一个很大的问题,还不会解决,只好另寻它法,为了让我以后不在犯同样的错误,把代码留在这里。!!!注意:这是教训,不是正确方法。
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int MAX=99999;int main(){int n,m,Max,m1,a[MAX],b[MAX];while(~scanf("%d %d",&m,&n)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);int t,j=1,v=1;t=n/m;while(j<m){Max=m1=0;for(int i=v;i<=t;i++){m1+=a[i];if(m1<0)m1=0;Max=max(Max,m1);}b[j++]=Max;v=t+1;t+=n/m;}//比如m=3,n=8 n/m=2 虽然n%m!=0,但是1 2    3 4   5 6   7 8在上边的while循环中竟然被分为了四段  哭晕 //第一次交没想到有n/m有余数的情况!!! 第二次就没有想到上面 7 8子段被重复计算且所分段数不符合要求的问题//最后想了想 可以用j来控制所分段数,让前m-1段和第m段分开算 Max=m1=0;for(int i=t-n/m+1;i<=n;i++){m1+=a[i];if(m1<0)m1=0;Max=max(Max,m1);}b[j++]=Max;int sum=0;for(int i=1;i<=m;i++)sum+=b[i];printf("%d\n",sum);}return 0;}

第三次提交前,发现我们不能硬生生的把这一串数字分为题目要求的子段个数,也就是说比如在这样一组数据3 8  1 3  -2  4  3 1  -5  2 ,按照我的方法会把该序列分成1  3,-2 4,3  1 -5  2。这样结果是4+4+4=12,很明显不对,只好找其他方法。看了两个小时这个链接里的解释,点击打开链接,点击打开链接才懂了,顺便把推理过程也放在这,省的忘了。现在就可以学习正确方法了。

题意:任意给你n个整数,让你求这n个整数的m个不相交子段和的最大值。

这道题是利用dp的思想,先定义 两个数组num[ n ],dp[ m][ n ],dp[i][j]表示由前j个数 ,组成的i个子段(最后一个子段包括num[ j ])的和的最大值。另外num[ j ]是否是自己组成一个子段。状态转移方程:dp[ i ][ j ]=max(dp[ i ][ j-1 ],dp[ i-1 ][ t])+num[ j ],1<=t<=j-1。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAX=1e6+5;int per_max[MAX],num[MAX];int DP(int m,int n)//DP是一种利用数组做题的思想,并不一定非得定义一个二维DP数组 {for(int i = 1 ;i <= m ;i ++){int tmp=0;for(int k = 1 ;k <= i ;k ++)tmp += num[k];per_max[n]=tmp;for(int j = i + 1 ;j <= n ;j ++){tmp = max (per_max[j-1],tmp)+num[j];//关键所在 per_max[j-1]=per_max[n];//这里赋给per_max[j-1],而不是per_max[j],就会把per_max[n]闲置出来留着优化per_max[n]= max(tmp,per_max[n]);}}return per_max[n];} int main(){int m,n;while(~scanf ("%d %d" ,&m ,&n)){for ( int i = 1 ;i <=n ;i++){scanf("%d",&num[i]);per_max[i]=0;}printf("%d\n", DP(m,n));}return 0;}
还有些地方理解的不够好,后面懂了再补充。

原创粉丝点击