HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)
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. ^_^
Process to the end of file.
题目大意:有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字段和)的更多相关文章
- 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 ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 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/ ...
- 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 ...
- 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 ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
随机推荐
- 基于matlab的傅里叶变换
原文出处https://blog.csdn.net/qq_37366291/article/details/79832886 例子1 作用:使用傅里叶变换找出隐藏在噪声中的信号的频率成分.(指定信号的 ...
- Directx11教程(21) 修正程序最小化异常bug
原文:Directx11教程(21) 修正程序最小化异常bug 很长时间竟然没有注意到,窗口最小化时候,程序会异常,今天调试水面程序时,随意间最小化了窗口,发现程序异常了.经过调试,原来程 ...
- 【水滴石穿】react-native-template-app
这个也是一个基础项目 地址如下https://github.com/ndlonghi/react-native-template-app 点击登陆跳转到首页 分析代码如 react-native-te ...
- PHPCMS快速建站系列之 pc标签where中如何使用变量
{pc:content action="lists" catid="$catid" where="typeid='$t'" order=&q ...
- vagrant up提示"Couldn't open file /path/to/base"的错误解决方法
在vagrant使用命令vagrant up启动虚拟机时 出错: C:\Vagrant>vagrant up Bringing machine 'default' up with 'virtua ...
- Android实战:手把手实现“捧腹网”APP(二)-----捧腹APP原型设计、实现框架选取
Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...
- 【New Feature】阿里云快照服务技术解析
一.背景 目前上云已经成为行业发展趋势,越来越多的企业级客户将业务系统和数据库迁移到云上.而传统的备份一体机/备份软件方式,并不适合云上ECS.RDS等产品的备份与容灾服务.阿里云块存储服务提供云 ...
- shell学习(17)- shell中2>&1的解释及输入输出重定向
大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...
- nodeJs学习-01 http模块
http模块基础: const http = require("http"); //引入http系统模块 var server = http.createServer(functi ...
- PHP笔试题(11道题)详解
题目一 <?php echo -10%3; ?> 答案:-1. 考查:优先级. 因为-的优先级比%求余的优先级低, 也就是-(10%3). 2 题目二: print (int)pow(2, ...