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

本文解析了一道名为MaxSumPlusPlus的算法题,旨在找出给定数列中m个不相交子段和的最大值。文章通过示例说明了问题难点,并提供了两种解决方案:一种是尝试直接分割数列的方法,另一种则是采用动态规划思想进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

Sample Output
  
  
6 8
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;
}
还有些地方理解的不够好,后面懂了再补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值