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/ ...
随机推荐
- sql基本
SELECT: select * from table select 列名 from table select DISTINCT 列名 from table INSERT: insert into t ...
- Python 面试中可能会被问到的30个问题
第一家公司问的题目 1 简述解释型和编译型编程语言? 解释型语言编写的程序不需要编译,在执行的时候,专门有一个解释器能够将VB语言翻译成机器语言,每个语句都是执行的时候才翻译.这样解释型语言每执行一次 ...
- Python全栈-magedu-2018-笔记10
第三章 - Python 内置数据结构 集set 约定 set 翻译为集合 collection 翻译为集合类型,是一个大概念 set 可变的.无序的.不重复的元素的集合 set定义 初始化 set( ...
- Mybatis获取传参
取自 https://blog.csdn.net/weixin_38303684/article/details/78886375 mybatis中SQL接受的参数分为:(1)基本类型(2)对象(3 ...
- day26:静态方法,类方法和反射
1,包内部不要去尝试应用使用相对导入,一定不会成功的,他不支持这个机制,包内导入时一定要用绝对导入 2,复习接口类,抽象类,Python中没有接口类,有抽象类,抽象类是通过abc模块中的metacla ...
- Django创建项目基本步骤
1.新建项目 django-admin startproject cmdb(项目名) 2.启动服务python manage.py runserver 0.0.0.0:8000(表示服务监听在8000 ...
- cesium 飞线 瓣体传感器(雷达扫描) 效果
参考:github地址 本人新手,npm webpack 这些还是一知半解,只记录自己得到成功结果的操作步骤,可能存在多余或错误的步骤. 1.github 把代码下载下来,解压. 2.webstorm ...
- k8s-No.2-pod学习
本章目录 pod结构图 pod语法及参数说明 pod声明周期 一 pod结构图 大部分情况下,Openshift中的Pod只是容器的载体,通过Deployment.DaemonSet.RC.Job. ...
- [ionic3.x开发记录]ng-content使用
在ionic开发公用组件的时候,我一直在想有没有angular有没有像vue一样的slot插槽.方便组件后期扩展. 然后去翻文档,发现有ng-content这么个东西,用法很像vue的slot. 组件 ...
- javascript 的引入
目录 一.静态引入 1. html标签script引入 2. esm 中import ModuleName from 'module/path' 3. commonjs 中 const ModuleN ...