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/ ...
随机推荐
- DevExpress GridControl复合表头(多行表头)设置
关于DevExpress.XtraGrid的复合表头或多行表头的示例,界面如下图所示 1.首先要把DevExpress的GridControl转换为BandedGridView 2.设置显示列及绑定的 ...
- connection reset by peer
connection reset by peer https 请求返回下面的内容 {"msg":"connection reset by peer"," ...
- sqlmap tamper下模块的使用
使用方法 根据实际情况,可以同时使用多个脚本,使用-v参数可以看到payload的变化. sqlmap.py -u "http://www.target.com/test.php?id=12 ...
- 学习ActiveMQ(八):activemq消息的持久化
1. 持久化方式介绍前面我们也简单提到了activemq提供的插件式的消息存储,在这里再提一下,主要有以下几种方式: AMQ消息存储-基于文件的存储方式,是activemq开始的版本默认的消息存储方式 ...
- Lambda Expression
Java 8的一个大亮点是引入Lambda表达式,使用它设计的代码会更加简洁.当开发者在编写Lambda表达式时,也会随之被编译成一个函数式接口.下面这个例子就是使用Lambda语法来代替匿名的内部类 ...
- Oracle归档开启和更改
运用 Xshell 客户端工具链接所在的 oracle 服务器 1.先进入数据库里面去 [root@DBSTANDBY ~]# su - oracle [oracle@DBSTANDBY ~]$ ...
- centos7设置静态IP地址
1.查看IP配置信息 ifconfig 如上图所示,我的em1网卡已配置好 2.编辑em1对应的配置文件,位于/etc/sysconfig/network-scripts/ifcfg-你的网卡名字 操 ...
- centos 7 挂载U盘
参考网址:https://blog.csdn.net/fengjunwang1980/article/details/78062838 1.首先使用fdisk -l命令查看磁盘情况 如果不知道哪一个是 ...
- python写算法中的栈
########### 栈的使用 ############### class StackFullError(Exception): pass class StackEmptyError(Excepti ...
- 某里巴巴Java工程师常规面试题以及解答
从HR弄来的P6-P7的JAVA工程师题目,分享给大家 1 Spring AOP和IOC的实现方法 http://blog.csdn.net/tarena_lixy/article/details/7 ...