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 "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 ≤ i y ≤ 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 and n, followed by n integers S 1, S 2, S 3 ... S n.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
Http
HDU:https://vjudge.net/problem/HDU-1024
Source
动态规划
题目大意
给出一个数列,求m段不相交的子区间使得区间和最大
解决思路
首先可以很快列出简单的动态转移方程,设Arr[i]表示原来数列中第i个数,设F[i][j]表示前j个数中选出i个区间的最大和。因为第i个数可以新开一组,也可以加入原来的一组中,所以有转移方程
\(F[i][j]=max(F[i][j-1]+Arr[j],max(F[i-1][(i-1)……(j-1)])+Arr[j])\)
(感谢@宫园薰指正方程中出现的错误)
因为题目中没有给出m的范围,而F[i]又只与F[i]前面的以及F[i-1]有关系,所以我们可滚动的做。
但这样还是会超时的,我们发现,转移的瓶颈在max(F[i][0……(j-1)])这里,即前面的最大值。而这是可以在推导F的时候一并记录下来的。所以我们可以设Pre_max[i]表示前i个中的最大值。那么转移方程就可以写成:
\(F[j]=max(F[j-1]+Arr[i],Pre\_max[j-1]+Arr[j])\)注意i那一维滚动掉了。
要注意信息的更新顺序。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int maxN=1000011;
const int inf=2147483647;
int n,m;
int Arr[maxN];
ll F[maxN];
ll Pre_max[maxN];
int main()
{
while (cin>>m>>n)
{
for (int i=1;i<=n;i++)
scanf("%d",&Arr[i]);
memset(F,0,sizeof(F));
memset(Pre_max,0,sizeof(Pre_max));
ll nowmax;
for (int i=1;i<=m;i++)
{
nowmax=-inf;//记录当前的max,方便更新Pre_max
for (int j=i;j<=n;j++)//注意这个循环中三个信息更新的先后顺序,另外这个j从i开始,因为要分出i组一定要至少有i个数
{
F[j]=max(F[j-1]+Arr[j],Pre_max[j-1]+Arr[j]);//新开一组,或加入到之前的最大的一组中去
Pre_max[j-1]=nowmax;//更新前面最大的
nowmax=max(nowmax,F[j]);//更新当前最大的
}
}
printf("%lld\n",nowmax);
}
return 0;
}
HDU 1024 Max Sum Plus Plus (动态规划)的更多相关文章
- HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]
Max Sum Plus Plus Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- 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个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
- 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 (动态规划、最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
随机推荐
- 将 C# 枚举序列化为 JSON 字符串 基础理论
该转换过程需要引用 Newtonsoft.JSON,这其中的转换过程还是蛮有意思的. 一.定义枚举 /// <summary> /// 托寄物品枚举 /// </summary> ...
- Linux df du 命令
df 命令 检查磁盘空间占用情况(并不能查看某个目录占用的磁盘大小). 命令格式:df [option] -h 以容易理解的格式(给人看的格式)输出文件系统分区使用情况,例如 10kB.10MB.10 ...
- React.js 入门与实战课程思维导图
原文发表于我的技术博客 我在慕课网的「React.js 入门与实战之开发适配PC端及移动端新闻头条平台」课程已经上线了,在这里分享了课程中的思维导图,供大家参考. 原文发表于我的技术博客 此导图为课程 ...
- Centos7部署elasticsearch并且安装ik分词以及插件kibana
第一步 下载对应的安装包 elasticsearch下载地址:https://www.elastic.co/cn/downloads/elasticsearch ik分词下载:https://gith ...
- 一个数据表通过另一个表更新数据(在UPDAT语句中使用FROM子句)
在sql server中,update可以根据一个表的信息去更新另一个表的信息. 首先看一下语法: update A SET 字段1=B表字段表达式, 字段2=B表字段表达式 from B WHE ...
- 《Linux内核设计与实现》课本第四章学习总结
进程调度 4.1 多任务 多任务操作系统就是能同时并发的交互执行多个进程的操作系统. 多任务系统分为两种: 抢占式多任务:Linux提供了抢占式的多任务模式,由调度程序来决定什么时候停止一个进程的运行 ...
- 非post请求时整个url作为参数传递出现bug
在非post请求使用整个url作为参数传递到后台时会出现url被截断的bug,这时通过encodeURIComponent进行url的编码可以解决.示例如下: <!--参数url-->Ur ...
- 使用PHP + Apache访问有错误的php脚本时不报错
遇到一个问题: 在命令行编辑php脚本后,直接使用php命令行执行该php脚本,如果脚本出现错误,在命令行的情况下会报错,显示错误信息,比如下面的情况. [root@localhost wwwroot ...
- Git—学习笔记1
Git是一种分布式版本控制工具,现阶段比较流行的版本控制工具主要分为:集中式版本控制工具盒分布式版本控制工具. 集中式版本控制工具:SVN和CVS为代表 集中式版本控制系统(每次都得从SVN服务器数据 ...
- [转帖]Nginx的超时keeplive_timeout配置详解
Nginx的超时keeplive_timeout配置详解 https://blog.csdn.net/weixin_42350212/article/details/81123932 Nginx ...