HDU 1024 Max Sum Plus Plus(最大M段和)
2016-12-03 16:14
337 查看
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26474 Accepted Submission(s): 9201
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.
Author
JGShining(极光炫影)
思路:
dp[i][j]表示分成i段前j个元素的最大和,同时这个最大和是由a[j]结尾的。
状态为i、j,值为dp[i][j]。
若a[j]与前面一段直接相连成为共同的一段,那么前面应该已经有了i段:
dp[i][j] = {dp[i][j-1] + a[j]}
若a[j]单独成一段,那么前面应该是已经有了i-1段:
dp[i][j] = {dp[i-1][k] + a[j]} (i-1<=k<j)
所以状态转移方程为
dp[i][j] = {max(dp[i][j-1], dp[i-1][k]) + a[j]} (i-1<=j<k)
有了状态转移方程,剩下的就是优化了,把状态转移表画出来。
首先对一些特殊情况做一些处理,定义出i,j的边界条件:
1、第i段可以以任何一个数字结尾;
2、某段以a[j]结尾的时候,表示前j个元素以j结尾的最大和,也就是说,最多只能分成j段(现在一共只有j个元素,最多的情况下就是每个数字单独成一段,那么刚好是j段),所以无论在什么情况下总有i<=j。
这里以题目中的 -1 4 -2 3 -2 3为例,状态转移表填写过程如下:
填写对角线元素的方法是,dp[i][j] = {dp[i-1][j-1] + a[j]} (i=j)
然后根据状态转移方程接着填
注意到,这里填每一行的时候,其状态仅与上一行有关。也就是说,仅仅依靠第i-1行就能够得到完整的第i行,同样,仅仅根据第i行就能填出i+1行,可以用滚动数组来优化空间,下面代码就用一个dp[2][M]大小的数组来完成整个表的填写。
#include <iostream> #include <cstring> #include <cstdio> #define M 1000010 #define INF 9999999 using namespace std; int a[M]; int dp[2][M]; int Max(int a, int b) { return a>b?a:b; } int main() { int m, n; while (scanf("%d %d", &m, &n)!=EOF) { for (int i=1; i<=n; i++) { scanf("%d", &a[i]); dp[0][i] = dp[1][i] = 0; } dp[0][0] = dp[1][0] = a[0] = 0; int t = 1; for (int i=1; i<=m; i++) { dp[t][i] = dp[1-t][i-1] + a[i]; int maxn = dp[1-t][i-1]; for (int j=i+1; j<=n-m+i; j++) { maxn = Max(maxn, dp[1-t][j-1]); dp[t][j] = Max(maxn, dp[t][j-1]) + a[j]; } t = 1-t; } t = 1-t; int ans = -1*INF; for (int i=m; i<=n; i++) { if (dp[t][i]>ans) ans = dp[t][i]; } cout << ans << endl; } return 0; }
相关文章推荐
- HDU - 1024 Max Sum Plus Plus(最大M段连续子段和,详细解释)
- hdu 1024 Max Sum Plus Plus(最大m子段和)
- HDU 1024 Max Sum Plus Plus(动态规划,给定一个数组,求其分成m个不相交子段和最大值的问题)
- hdu 1024 Max Sum Plus Plus (求一个序列中选出的m个不相交子段和的最大值)
- 【m段最大连续子段和的和】HDU - 1024 Max Sum Plus Plus
- ☆HDU 1024 Max Sum Plus Plus 绝对能看懂得题解(难题,多个子段和的和最大)
- hdu 1024 Max Sum Plus Plus(最大M子段和)
- HDU 1024 Max Sum Plus Plus(最大M子段和)
- hdu 1024 Max Sum Plus Plus(动态规划+m子段和的最大值)
- 【HDU 1024】 Max Sum Plus Plus【动态规划求最大M子段和详解-好题 】
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
- hdu 1024 Max Sum Plus Plus(dp求m个不相交子段和的最大值)
- HDU 1024 Max Sum Plus Plus[dp](最大m子段和)
- HDU 1024 Max Sum Plus Plus (最大和子序列增强版:求规定下标内的最大值)
- HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和
- HDU 1024 Max Sum Plus Plus(动态规划,给定一个数组,求其分成m个不订交子段和最大值的题目)
- hdu 1024 Max Sum Plus Plus(DP最大字段和)
- hdu 1024 Max Sum Plus Plus(最大m字段和)
- HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
- HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和