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. Hdu 1007 最近点对

    题目链接 Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. 封装函数通过输入(元素,属性,目标值)改变div样式

    ## 假设一个div样式如下```html<!DOCTYPE html><html lang="en"> <head> <meta cha ...

  3. 手机monkey测试BUG重现及解决方法

    目录 1.1 Monkey测试简介...1 1.2 Monkey程序介绍...1 1.3 Monkey命令的简单帮助...2 1.4 Monkey命令参数介绍...2 1.5 Monkey测试步骤.. ...

  4. material-ui里面的withStyles是什么?

    export default withStyles(styles, { name: 'MuiAppBar' })(AppBar); //这里的作用是什么? withStyles 是一个 HOC 组件, ...

  5. 【JZOJ4861】【NOIP2016提高A组集训第7场11.4】推冰块

    题目描述 Dpstr最近迷上了推冰块.冰地是一个n行m列的网格区域,第i行第j列的格子记为(i,j),也就是左上角为(1,1),右下角为(n,m).每个格子可能是冰面.障碍物.减速带三者之一.其中,冰 ...

  6. 《C程序设计语言》笔记(三)

    六:结构 1:结构体声明中,比如: struct point{ int x; int y; }; struct后面的名字是可选的,称为结构标记.结构成员.结构标记和普通变量可以采用相同的名字,它们之间 ...

  7. 提供SaaS Launchkit,快速定制,一云多端等能力,一云多端将通过小程序云实现

    摘要: SaaS加速器的技术中心能力中,将提供SaaS Launchkit,快速定制,一云多端等能力,加速应用上云迁移.降低应用开发和定制的门槛,提升效率.其中非常关键的一云多端能力将通过小程序云实现 ...

  8. 自定义View系列教程03--onLayout源码详尽分析

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  9. 阿里云应用实时监控 ARMS 再升级,支持 Prometheus 开源生态

    摘要: 应用实时监控服务 (ARMS) 是一款APM类的监控产品. 用户可基于 ARMS 的前端.应用.自定义监控,快速构建实时的应用性能和业务监控能力.ARMS 让所有性能问题“一屏了然”,不遗余力 ...

  10. python特性(八):生成器对象的send方法

    生成器对象是一个迭代器.但是它比迭代器对象多了一些方法,它们包括send方法,throw方法和close方法.这些方法,主要是用于外部与生成器对象的交互.本文先介绍send方法. send方法有一个参 ...