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/ ...
随机推荐
- python多进程使用及线程池的使用方法
多进程:主要运行multiprocessing模块 import os,time import sys from multiprocessing import Process class MyProc ...
- 字符串Hash || BZOJ 3555: [Ctsc2014]企鹅QQ || P4503 [CTSC2014]企鹅QQ
题面:[CTSC2014]企鹅QQ 题解:无 代码: #include<iostream> #include<cstring> #include<cstdio> # ...
- Head First Python-python面向对象
与大多数其他的编程语言一样,Python容许创建并定义面向对象的类,类可以将代码与代码处理的数据相关联. 对于更加复杂的数据,一般的列表已经不能满足需求了. 我们可以使用字典dict将数据值与键相关联 ...
- Jmeter简单的接口测试举例
推荐文章:http://www.cnblogs.com/puresoul/p/5092628.html 1.创建线程组 本次测试模块为一个线程组(可以在线程组内列出模块内的需要测试的接口) 2.在线程 ...
- HTTPS请求
hhtps:HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 HTTP通道,简单讲是HTTP的安全版,即 ...
- Spring Boot(二):Spring-Data-JPA操作数据库( Hibernate)增删改查
一.Maven使用3.3.9版本或以上,选择Binary 版本 二.添加spring-data-jpa和数据库依赖,以oracle为例 三.添加连接数据库配置 四.新建model自动生成数据库表(不用 ...
- ADB——命令大全
基本语法 基本语法 adb [-d|-e|-s <serialNumber>] <command> # serialNumber表示设备序列号,也可以是ip地址 # 如果只有一 ...
- 初尝Spring Cloud Config
1,下载源码 地址https://spring.io/guides/gs/centralized-configuration/ 2,导入工程 解压后分别把Server端与Client端导入到两个Ecl ...
- Linux系统常用升级的基础包
Linux系统常用升级的基础包 yum -y install lrzsz gcc gcc-c++ make flex autoconf automake vixie-cron libjpeg libj ...
- IBM服务器安装Ubuntu Linux server 64以及网络配置
最近在部署AC环境,云AC要求软件环境为Ubuntu 14.04 版本的服务器Linux操作系统,下面是环境部署的准备工作: 一.下载文件 (1)下载系统文件 地址:http://mirrors.16 ...