动态规划 hdu 1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41033 Accepted Submission(s):
14763
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 S1,
S2, S3, S4 ...
Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤
Sx ≤ 32767). We define a function sum(i, j) =
Si + ... + Sj (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(i1, j1) + sum(i2,
j2) + sum(i3, j3) + ... +
sum(im, jm) maximal (ix ≤
iy ≤ jx or ix ≤
jy ≤ jx 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(ix, jx)(1 ≤ x ≤ m) instead.
^_^
followed by n integers S1, S2,
S3 ... Sn.
Process to the end of
file.
line.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended;
int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m;
arr[ ]储存输入的数;
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
用temp来找出 j 个数取 k 组所得的组的最大和;同时把它记入在pre[ ]中,用于进行下次更新;
dp[j]=max(dp[j-1],pre[j-1])+arr[j];此时dp[j]和dp[j-1]有同样的组数,
dp[j]=dp[j-1]+arr[j]; // 表示把第j个数加入dp[j-1]的其中一组,能保持组数不变;
dp[j]=pre[j-1]+arr[j]; // 表示让第j个数独成一组,再加上比dp[j]少一组的组集中的最大的;
两者中选择大的组合方式;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
const int inf=0x3f3f3f3f; int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m; int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&arr[i]);
}
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return ;
}
动态规划 hdu 1024的更多相关文章
- 动态规划-----hdu 1024 (区间连续和)
给定一个长度为n的区间:求m段连续子区间的和 最大值(其中m段子区间互不相交) 思路: dp[i][j]: 前j个元素i个连续区间最大值 (重要 a[j]必须在最后一个区间内) 转移方程:dp[i][ ...
- 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 ...
- 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+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 怒刷DP之 HDU 1024
Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 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" ...
- 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 Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Java并发之Thread类的使用
一.线程的几种状态 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以下这几个状态:创建(new).就绪(runnable).运行(running).阻塞(blocked).time wa ...
- laravel5.4 导出 Excel 表格
1.执行 composer require maatwebsite/excel 2. composer.json 文件出现(或者手动添加) 3.在config目录下 app.php 添加参数 4.导出 ...
- 【全网最全的博客美化系列教程】08.自定义地址栏Logo
全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...
- celery 大量消息的分布式系统 定时任务
Celery 1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 https://www.jia ...
- Java集合图谱
比较 是否有序 是否允许元素重复 Collection 否 是 List 是 是 Set AbstractSet 否 否 HashSet TreeSet 是(用二叉排序树) Map AbstractM ...
- spring-boot-starter-thymeleaf 避坑指南
第一步:pom配置环境 先不要管包是做什么的 总之必须要有 否则进坑 <!--避坑包--> <dependency> <groupId>net.sourceforg ...
- 一次ES故障排查过程
作者:莫那鲁道 原文:http://thinkinjava.cn/#blog 某天晚上,某环境 ES 出现阻塞, 运行缓慢.于是开始排查问题的过程. 开始 思路:现象是阻塞,通常是 CPU 彪高,导致 ...
- Jmeter_24个常用函数
JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便. JMeter函数是一种特殊值,可用于除测试计划外的任何组件. 函数调用的格式如下所示:${__functionName(var1 ...
- 论文阅读笔记四十五:Region Proposal by Guided Anchoring(CVPR2019)
论文原址:https://arxiv.org/abs/1901.03278 github:code will be available 摘要 区域anchor是现阶段目标检测方法的重要基石.大多数好的 ...
- Three teachers who make differences in my life
人生路漫漫,从小到大,经历过九年义务教育的我们也受到过很多老师的熏陶,而已经进入大学的我也会回想起那些给我带来别样意义的老师们. 亲爱的老李-老李是我初中的班主任,也是我的历史老师.依稀记得上他的课会 ...