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 ...
随机推荐
- 24种编程语言的Hello World程序
24种编程语言的Hello World程序 这篇文章主要介绍了 24 种编程语言的 Hello World 程序,包括熟知的 Java.C 语言.C++.C#.Ruby.Python.PHP 等编程语 ...
- 【JZOJ4924】【NOIP2017提高组模拟12.17】向再见说再见
题目描述 数据范围 =w= 设h[i]表示,甲队得到i分的方案数. 那么h[(n+k)/2]和h[(n−k)/2]就是答案. 设g[i]表示,甲队得到至少i分的方案数. 那么h[i]=g[i]−∑j& ...
- 连接池dbcp
连接池dbcp DBCP:apache组织 使用步骤: 1.导入jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar.commons-logging-1.2 ...
- PLAY2.6-SCALA(三) 数据的返回与保存
1.修改默认的Content-Type 自动设置内容类型为text/plain val textResult = Ok("Hello World!") 自动设置内容类型为appli ...
- hdu3472 混合图判断欧拉通路
对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不 ...
- 洛谷4178 BZOJ1468 Tree题解点分治
点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...
- 基于MaxCompute的数仓数据质量管理
声明 本文中介绍的非功能性规范均为建议性规范,产品功能无强制,仅供指导. 参考文献 <大数据之路——阿里巴巴大数据实践>——阿里巴巴数据技术及产品部 著. 背景及目的 数据对一个企业来说已 ...
- MongoDB -- JAVA基本API操作
package com.example.mongodb.mongodb.demo; import com.mongodb.MongoClient; import com.mongodb.client. ...
- python---异常处理与反射
一.异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! try: pass except ...
- HDU3844 Mining Your Own Business
HDU3844 Mining Your Own Business 问题描述John Digger是一个大型illudium phosdex矿的所有者.该矿山由一系列隧道组成,这些隧道在各个大型交叉口相 ...