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" 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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤
1,000,000, -32768 ≤ S x ≤ 32767). We
define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ iy ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m)
instead. ^_^
Input
Each test case will begin with two integers m
Output
Output the maximal summation described above in one
line.
and n, followed by n integers S 1,
S2, S 3 ... S n.
Process to the end of file.
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
Hint
Huge input, scanf and dynamic programming is
recommended.
分析:d[j]为遍历到当前数字时,前j-1个数的i段区间和的最大值加上a[j],d[j]是动态的,可取可不取;
p[j]为记录前j个数的i段区间和的最大值;
j |
1 |
2 |
3 |
4 |
5 |
6 |
|
i |
-1 |
4 |
-2 |
3 |
-2 |
3 |
|
1 |
d |
-1 |
4 |
2 |
5 |
3 |
6 |
1 |
p |
-1 |
4 |
4 |
5 |
5 |
|
2 |
d |
-1 |
3 |
2 |
7 |
5 |
8 |
2 |
p |
-1e9 |
3 |
2 |
7 |
7 |
代码中p[n]没有记录,直接放在了ans中。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using
namespace std;
const int
Max=1e6+5;
int
p[Max],a[Max],d[Max];
int main()
{
int m,n,ans;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(p,0,sizeof(p));
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
ans=-1e9;
for(int j=i;j<=n;j++)
{
d[j]=max(d[j-1],p[j-1])+a[j];
p[j-1]=ans;
ans=max(ans,d[j]);
}
}
printf("%d\n",ans);
}
}
Max Sum Plus Plus HDU - 1024的更多相关文章
- 最大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 ...
- 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个必 ...
- 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 ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- 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子段和详解 】
Max Sum Plus Plus Time 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 ...
- (最大m子段和) Max Sum Plus Plus (Hdu 1024)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- python2和python3 安装pip冲突问题
系统:win10 问题:安装python2和Python3后 在cmd 中 pip和python命令会冲突 原因:先安装的python3,python3会自动配置path安装pip,pip和pytho ...
- 源码包安装maven
含有的命令:cd,tar,mv,vim,sourc,mvnpv [root@ycj ~]# cd /usr/local/src/ //切换路径 [root@ycj src]# wget http:// ...
- jmeter使用指南:jmeter无脑式指南
一:启动jmeter 二:添加线程组 三:添加 HTTP 请求 四:添加监听器 五:填写访问的域名,并保存 六:运行,查看结果 七:配置多线程.循环机制,进行压力测试
- 解决vue webApp使用lib-flexible和px2rem引用第三方ui库后,样式变小问题
首先,需要卸载项目中的postcss-px2rem. npm uninstall postcss-px2rem --save-dev 其次,安装postcss-px2rem-exclude npm i ...
- body里面的onload和window.onload,window.load的区别
区别:body里面的onload是在“页面加载完成后执行的动作”window里面的onload是在“页面加载时执行的动作” window.load这个应该只是表明事件方法,但并未执行,比如click表 ...
- mongodb 参数优化
1.大部分IO操作为随机IO,建议采用SSD或PCIE,普通硬盘RAID10 2.IO调度算法.普通磁盘:deadline避免IO请求出现“饥饿”现象,SSD/PCIE:noop简单的先进先出处理请求 ...
- [dev][dpdk][crypto] dpdk加解密设备与IPSEC
概述 分三部分,加解密框架(crypto framework),加解密设备(crypto dev),安全协议(Security Framework) × API,设计思路等,都在加解密框架里:见文档 ...
- Linux 文件系统下的压缩、解压命令
参考文献:Linux下的tar压缩解压缩命令详解 - 智昕 - 博客园
- anaconda安装tensorflow报错 No module named 'tensorflow'解决方法(windows)
这个错误的原因可能是,anaconda安装的python版本为3.7,现在tensorflow仅支持python 3.6 改变python版本:首先在命令行创建一个名为python36的环境,指定 ...
- python中闭包的理解
闭包的三个条件: 1.函数(外函数)中定义了内函数:2.内函数使用了外函数的非全局变量:3.外函数最终返回的是内函数的引用. 简单闭包事例: #outerfunc为外函数 def outerfunc( ...