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

题目大意:有n的个数,需要你将他划分成m段互不相交的子段,求最大的子段和

 思路:在这里向大家推荐一篇大佬的博客,讲解的清晰明了,让我获益匪浅 https://blog.csdn.net/winter2121/article/details/72848482  Orz

  大佬的博客写的很清晰,而且附上图解很容易弄懂

  dp[i][j] 代表前i个数并且以第i个数为末尾划分成了j段的最大和。

  而dp[i][j] 有两种情况:1. 将第i个数划分到前面这一段取

            2.将第i个数单独拿出来作为一段的开始

  dp[i][j] = max(dp[i-1][j],max(前i个数中的最大和))+a[j];

 #include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = ;
LL n, m;
LL dp[maxn][], a[maxn];//dp[i][j]代表以第i个数为结尾且分成j段的最大和
int main()
{
ios::sync_with_stdio(false);
while (/*cin>>m>>n*/scanf("%lld%lld", &m, &n) != EOF) {
for (int i = ; i <= n; i++)scanf("%lld", &a[i]);
/*cin >> a[i];*/
for (int i = ; i <= n; i++)dp[i][] = dp[i][] = ;
for (int i = , k = ; i <= m; i++, k ^= ) {
LL maxpre = -INF; dp[i - ][k] = -INF;//避免与后面for循环的影响
for (int j = i; j <= n - m + i; j++) {
maxpre = max(maxpre, dp[j-][k ^ ]);//寻找上一行第i个数之前的最大值
dp[j][k] = max(dp[j - ][k], maxpre) + a[j];
}
}
LL ans = -INF;
for (int i = m; i <= n; i++)
ans = max(ans, dp[i][m&]);
//cout << ans << endl;
printf("%lld\n", ans);
}
return ;
}
 

HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)的更多相关文章

  1. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

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

  2. hdu 1024 Max Sum Plus Plus (动态规划)

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

  3. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  4. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  5. 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/ ...

  6. 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 ...

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

  8. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

随机推荐

  1. Spring简介+HelloWorld

    IoC Container: 这是最重要的,也是最基础的, Spring的基础.它的作用是配置和Java对象的生命周期管理. DAO, ORM, AOP, WEB: 该模块可用于将工具或框架集成到了S ...

  2. 前端规范2-CSS规范

    CSS规范 缩进 使用Tab缩进(相当于四个空格) 选择器与{之间必须包含空格,参1 属性名和之后的:不允许包含空格,:与属性值之间必须包含空格.      例 列表性属性值在单行时,后必须跟一个空格 ...

  3. JavaScript--查看代码运行效率console.time()与console.timeEnd()用法

    程序运行时间计算: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. MUI - sortable在mui.js前端框架不兼容的解决方案

    关于sortable看这 兼容的解决方案看这 http://www.cnblogs.com/phillyx/ 示例代码已更到github

  5. Java练习 SDUT-2787_加密术

    加密术 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 加密技术是一种常用的安全保密手段,利用加密技术可以把重要的数据变 ...

  6. phpexcel使用说明4

    <div class="postBody"> <div id="cnblogs_post_body"><p>PHPExcel ...

  7. C#中的字段,常量,属性与方法

    以前是学C++的,初次学微软的C#头都大了.什么字段,常量,属性,方法......微软把别人的东西拿来糅合在C#里,弄成了一个“大杂烩”.其实,说到底,“字段”不就是“变量”吗,所谓的“方法”不就是“ ...

  8. HDU 5584 LCM Walk【搜索】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 分析: 这题比赛的时候卡了很久,一直在用数论的方法解决. 其实从终点往前推就可以发现, ...

  9. 51nod 1686 第K大区间【离散化+二分】

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 定义一个区间的值为其众数出现的次数. 现给出n ...

  10. 在沙箱中IE不能上网的解决方法

    近期在解决一个问题,在我们的沙箱中IE不能上网 现象:     IE不能上网.输入www.baidu.com 提示:不能查找到DNS.也不能ping 通     其它浏览器上网没有问题(SG浏览器,C ...