(最大m子段和) Max Sum Plus Plus (Hdu 1024)
Total Submission(s): 23844 Accepted Submission(s): 8143
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.
2 6 -1 4 -2 3 -2 3
8
这里的最小情况是只分成一段的时候,就退化为最大子段和问题了,这个是段数的最小情况了; 如果只有0个数的时候,结果肯定为零了,或者如果只有一个数的时候就是这个数了,那么数列只有0个或者1个的时候就是数组的最小情况了。
然后记录使用一个数组记录dp[MAX_N],其中dp[i]的含义就是在一定选择i这个数的时候得到的最大和值。
这样如果分成i段,有j个数的时候dp[j] = max(dp[j-1]+arr[j], MAX[j-1]+arr[j]),其实如果使用二维数组,那么MAX[j-1] == dp[[i-1][j-1]这个状态格的数值,表示分成i-1段的时候,有j-1个数的值。取max就是表示arr[j]是直接和dp[j-1]即接在后面第i段,还是独立成为一段的值比较大,选择最优方案,取最大值。
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std; const int MAX_N = ;
int dp[MAX_N], MAX[MAX_N], arr[MAX_N]; int main()
{
int n, m, maxSum;
while (~scanf("%d %d", &m, &n))
{
memset(MAX, , sizeof(int) * (n+));
memset(dp, , sizeof(int) * (n+));
for (int i = ; i <= n; i++)
{
scanf("%d", arr+i);
}
for (int i = ; i <= m; i++)
{
maxSum = INT_MIN;
for (int j = i; j <= n; j++)
{
dp[j] = max(dp[j-]+arr[j], MAX[j-]+arr[j]);
MAX[j-] = maxSum;
maxSum = max(maxSum, dp[j]);
}
}
printf("%d\n", maxSum);
}
return ;
}
(最大m子段和) Max Sum Plus Plus (Hdu 1024)的更多相关文章
- 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" ...
- 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- 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 ...
- 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个必 ...
- 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]
[题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...
- 最大子段和(Max Sum)
Max Sum. The following is an instance. a) (-2,11,-4,13,-5,-2) 思路: 最大子段和:给定一个序列(元素可正可负),找出其子序列中元素和 ...
- HDOJ-1003 Max Sum(最大连续子段 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=1003 给出一个包含n个数字的序列{a1,a2,..,ai,..,an},-1000<=ai<=100 ...
- [ACM] hdu 1003 Max Sum(最大子段和模型)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 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 ...
随机推荐
- 自定义 Mysql 类 与 自定义 异常类
import MySQLdb class MyExcept(Exception): ''' 常见做法定义异常基类,然后在派生不同类型的异常 ''' def __init__(self, *args): ...
- Linux_(1)基本命令(上)
一.基本命令1.我是谁 whoami --who am i2.谁在线 who w3.显示当前路径(定位) pwd4.切换目录 cd ~返回主目录 cd ..返回上一级目录5.查看某个目录中的子目录和文 ...
- (转)system.badimageformatexception 未能加载文件或程序集
“/xxxxx”应用程序中的服务器错误. ------------------------------------------------------------------------------- ...
- phpstorm的安装和使用
1下载phpstorm #下载地址 https://www.jetbrains.com/phpstorm/ 2激活并安装 create associations 表示的是以后编辑选择的文件也默认用id ...
- Tech 2 doesn’t system for H2 above 2007
I purchased my Tech2 from obd2tool.com and it operates excellent. Can not definitely compare softwar ...
- RNA Sequencing
RNA Sequencing 选择其他单元: RNA Sequencing 国内领先的转录组定序基地 Transcriptome Sequencing可全面性并快速地获得某一物种特定细胞或组织在某一状 ...
- 洛谷1066 2^k进制数
原题链接 大力猜结论竟然猜对了.. 对于一对\(k,w\),我们可以把\(w\)位划分成\(k\)位一段的形式,每一段就是转换成十进制后的一位,这个从题面的解释中应该可以理解. 先不考虑可能多出(即剩 ...
- Carbon document
< Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inh ...
- Python实现的复杂的计算器的代码
用Python实现复杂的计算器,可以按照“()”.乘除.加减的优先级进行混合运算.主旨是对正则表达式进行学习. 设计思路: 1.在计算式中搜索最后一个“(”,再搜索和它匹配的“)”,截取两个括号间的表 ...
- css字符串转换为类map对象及反转
存储对象为啥是类map(即:{key:val,...}格式),因为Map对象的val为字符时,无法存储 '('.')' 左右括号,我也很无奈╮(╯▽╰)╭ 解析脚本: <!DOCTYPE htm ...