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. ^_^

InputEach 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.
OutputOutput 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.

思路:最大m段子段和问题,设sum[n][m]为前n个数,m段子段和的最大值,状态转移有三:
1.a[n]不属于该第m段最大和,sum[n][m] = sum[n-1][m]
2.a[n]属于该第m段最大和,且为新的一段 sum[n][m] = sum[n-1][m-1] + a[n]
3.a[n]属于该第m段最大和,且不为新的一段 sum[n][m] = sum[n-1][m] + a[n]
由状态转移方程易知,1与3互相矛盾,就要寻找第二个状态转移方程来辅助计算,令b[n][m]为包含a[n]的前n个数的m段最大子段和,则sum[n][m]为前n个数的m段最大子段和,a[n]不一定包含在内,据此知,sum[n][m] = max(b[j][m])(m<=j<=n),最终答案就是sum[n][m]
由上述分析,可得出b[n][m]的状态转移方程,a[n]属于第m段/不属于第m段,即b[n][m] = max(b[n-1][m], sum[n-1][m-1]) + a[n],通过该式可以得出计算顺序,先计算b[n][m],其中需要sum[n-1][m-1]与b[n-1][m],再更新sum[n][m]),而在计算sum[n][m] = max(b[j][m])(n<=j<=m),类比完全背包的优化,在计算sum[n][m]时,sum[n][m] = max(b[j][m])(m<=j<=n) = max(b[n][m], sum[n-1][m]),因为sum[n-1][m] = max(b[x][m])(m<=x<=n-1),等式成立,我们枚举的顺序是从小到大,所以在sum[n][m]计算之前sum[n-1][m]已经计算好了,不需要再重复枚举了
const int maxm = ;

int sum[maxm][maxm], b[maxm][maxm], a[maxm];

int main() {
ios::sync_with_stdio(false), cin.tie();
int n, m;
int tmp;
while(cin >> m >> n) {
memset(sum, , sizeof(sum)), memset(a, , sizeof(a)), memset(b, , sizeof(b));
for(int i = ; i <= n; ++i) cin >> a[i];
for(int i = ; i <= m; ++i) {
for(int j = i; j <= n; ++j) {
b[j][i] = max(b[j-][i], sum[j-][i-]) + a[j];
sum[j][i] = max(sum[j-][i], b[j][i]);
}
}
cout << sum[n][m] << endl;
}
return ;
}
但是注意,该题的范围是1e6,二维显然空间炸了,就需要优化为一维的滚动数组形式,我们将计算的式子都列出来:
b[n][m] = max(b[n-1][m], sum[n-1][m-1]) + a[n], sum[n][m] = max(sum[n-1][m], b[n][m])
滚动时从小到大,该位置为j,则j之前的是[][m],j之后的是[][m-1](上一轮的),则先计算b[],再更新sum[],因为sum和b有冲突,先sum[n-1][m-1],再sum[n-1][m],就用一个tmp来记录
const int maxm = 1e6+;

int sum[maxm], b[maxm], a[maxm];

int main() {
ios::sync_with_stdio(false), cin.tie();
int n, m;
int tmp;
while(cin >> m >> n) {
memset(sum, , sizeof(sum)), memset(a, , sizeof(a)), memset(b, , sizeof(b));
for(int i = ; i <= n; ++i) cin >> a[i];
for(int i = ; i <= m; ++i) {
tmp = -0x3f3f3f3f;
for(int j = i; j <= n; ++j) {
b[j] = max(b[j-], sum[j-]) + a[j]; // 此处sum[j-1] 是第i-1段
sum[j-] = tmp; // 更新sum[j-1] 为第i段的, 该tmp是第i段的j-1,由上一次的循环继承而来
tmp = max(sum[j-], b[j]); // 更新tmp, 该tmp是第i段的j,为下一次循环中的的j-1
}
sum[n] = tmp;
}
cout << sum[n] << "\n";
}
return ;
}

参考微博:https://www.cnblogs.com/chuckcpc/p/dp_Max_M_Sum.html

最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024的更多相关文章

  1. Max Sum Plus Plus HDU - 1024

    Max Sum Plus Plus     HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...

  2. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  3. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  4. 洛谷P1121 环状最大两段子段和

    题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的第一行是一个正整数N,表示了序列 ...

  5. HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. 洛谷 P1121 环状最大两段子段和 解题报告

    P1121 环状最大两段子段和 题目描述 给出一段环状序列,即认为\(A_1\)和\(A_N\)是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 第一行是一个正整数 ...

  7. (最大m子段和) Max Sum Plus Plus (Hdu 1024)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024     Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  8. HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  9. hdu 1024 Max Sum Plus Plus (子段和最大问题)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. Hackintosh相关资源站

    猫叔博客 https://www.maoshu.cc/ 大神RehabMan https://bitbucket.org/RehabMan/ dsdt/ssdt打补丁译文 https://blog.c ...

  2. mybatis(五):源码分析 - 结果集映射流程

  3. opensuse安装pycharm

    最近在学习python,所以查询了很多资料,大多都推荐pycharm进行pychon项目开发.于是查阅一些资料,整理出这个安装步骤.(仅供参考!!!仅供参考!!!仅供参考!!!) 仅供参考!!!仅供参 ...

  4. HTML的文档设置标记

    1.格式标记 <br/> 强制换行标记 <p> 换段落标记 换段落,由于多个空格和回车在HTML中会被等效为一个空格,所以HTML中要换段落就要用<p>,<p ...

  5. TD-在http请求头上添加参数

    dojo.xhrGet({ url : url, timeout : easy7.timeout, headers: { iBaseToken: easy7.iBaseToken },

  6. redis缓存处理机制

    1.redis缓存处理机制:先从缓存里面取,取不到去数据库里面取,然后丢入缓存中 例如:系统参数处理工具类 package com.ztesoft.iotcmp.utils; import com.e ...

  7. python3练习100题——006

    继续做题-经过py3测试 原题链接:http://www.runoob.com/python/python-exercise-example6.html 题目:斐波那契数列. 我的代码: def fi ...

  8. jvm(2):垃圾收集和内存分配

    typora-root-url: ./ 垃圾收集 垃圾收集器关注的是线程共享的这部分内存. jvisualvm用来监控JVM的运行情况,可以用它来查看和浏览Heap Dump.Thread Dump. ...

  9. C++-HDU1166-敌兵布阵[数据结构][树状数组]

    单点修改+区间查询=树状数组 空间复杂度O(n) 时间复杂度O(mlogn) #include <set> #include <map> #include <cmath& ...

  10. 题解【洛谷P1841】[JSOI2007]重要的城市

    题面 题解 最短路图模板题. 介绍一下最短路图: 先对原图跑一边单源最短路,求出源点到每个点\(i\)的最短路\(dis[i]\). 接下来构建新图:对于一条边\((x,y,v)\),若\(dis[x ...