0
点赞
收藏
分享

微信扫一扫

hdoj Max Sum Plus Plus 1024 (DP) m个连续数组最大和

朱小落 2023-04-19 阅读 107


Max Sum Plus Plus


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



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


Hint


//题意:


给你一个m,n,然后给你n个数,表示让你从n个数中找出m个连续的数组使得他们的和最大。


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x80000000
using namespace std;
int a[1000010];
int b[1000010];
int dp[1000010];
int main()
{
	int n,m,i,j,k;
	int mm;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(i=0;i<=n;i++)
		{
			dp[i]=0;
			b[i]=0;
		}
		for(i=1;i<=m;i++)
		{
			mm=INF;
			for(j=i;j<=n;j++)
			{
				if(dp[j-1]>b[j-1])
					dp[j]=dp[j-1]+a[j];
				else
					dp[j]=b[j-1]+a[j];
				b[j-1]=mm;
				if(mm<dp[j])
					mm=dp[j];
			}
			b[j-1]=mm;
		}
		printf("%d\n",mm);
	}
	return 0;
}


举报

相关推荐

0 条评论