Max Sum Plus PlusTime 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 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
Sample Output
|
题意:给你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 PlusTime 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 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
Sample Output
|
hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp
原文:http://blog.csdn.net/u013532224/article/details/44657349