hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp



Max Sum Plus Plus

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


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.
 



题意:给你n个顺序排列的数字, 要求你找m段数字和,要求这m段数字和  的和 最大。 一段数字最少包含一个数。

做法:  滚动数组 dp。 dp[i][j][k], i表示第几个数,因为计算第i个数的时候只需要考虑第i-1个数 达成的情况。所以这一维可以用滚动数组。j表示 已经有几段数字了。 k表示第i个数字取了,或者没有取。 取不取的区别在于,如果取了那么可以继续在这个段里添加数字,如果没有取,那么下一个数字如果添加进来,那么肯定是有了新的一段。 

状态转移方程:

当前要是不取,那么可以直接从上一个数字的有取或者没取中 选较大的值来转移:

dp[cur][j][0]=max(dp[cur^1][j][0],dp[cur^1][j][1]);     

当前要是取了,上一个数也 取了的话 ,那么当前 这个数 可以 增加在这个段里面:

dp[cur][j][1]=max(dp[cur][j][1],dp[cur^1][j][1]+num[i]);

当前取了,上一个数不管 是没有取,或者取了,都可以让这个数做为 新增段的第一个数字:

dp[cur][j][1]=max(dp[cur][j][1],max(dp[cur^1][j-1][0]+num[i],dp[cur^1][j-1][1]+num[i])); 




int num[1000010];
int dp[2][1000010][2];//n,m,1取 0没取
int main()
{
	int n,m;
	while(cin>>m>>n)
	{
		for(int i=0;i<n;i++)
			cin>>num[i]; 
		for(int i=0;i<=m;i++)
			dp[0][i][0]=dp[0][i][1]=-999999999;
		dp[0][0][0]=0;

		int cur=0;
		for(int i=0;i<n;i++)
		{ 
			cur^=1;
			for(int j=0;j<=m;j++)
				dp[cur][j][0]=dp[cur][j][1]=-999999999;

			for(int j=0;j<=m;j++)
				dp[cur][j][0]=max(dp[cur^1][j][0],dp[cur^1][j][1]);

			for(int j=0;j<=m;j++)
				dp[cur][j][1]=max(dp[cur][j][1],dp[cur^1][j][1]+num[i]);

			for(int j=1;j<=m;j++)
				dp[cur][j][1]=max(dp[cur][j][1],max(dp[cur^1][j-1][0]+num[i],dp[cur^1][j-1][1]+num[i])); 

		}
		printf("%d\n",max(dp[cur][m][0],dp[cur][m][1]));

	}
	return 0;
}











Max Sum Plus Plus

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


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.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值