HDU1024_Max Sum Plus Plus【滚动数组】
Problem 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 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. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
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
Hint
Huge input, scanf and dynamic programming is recommended.
Author
JGShining(极光炫影)
题目大意:给你两个数M和N,之后是N个数,从这N个数找到M个子段,
求M个子段的最大和
思路:一開始不懂怎么找状态转移方程。
參考别人博客才明确。
.设dp[i][j] 为将前 j 个数字分成 i 段的最大和。num[j]为当前数字
那么转移方程为 dp[i][j] = max(dp[i][j-1]+num[j],dp[i-1][k]+num[j]) (i-1<=k<=j-1)
也能够视为 dp[i][j] = max(dp[i][j-1]+num[j],max(dp[i-1][i-1],dp[i-1][i],…,dp[i-1][j-1])
)
//注:max(dp[i-1][i-1],dp[i-1][i],…,dp[i-1][j-1]) 就是dp[i-1][j-1]
意思是:前 j 个数字分成 i 段的最大和有两个决策。
1、将当前第j个数字并入第i段,与第j-1个数字所在的一段并为一段的最大和。
2、将当前第j个数字作为第i段。而第k个数字所在的一段为第i-1段,区间(k+1,j-1)的数字
不再选则的最大和。
取这两个决策中最大值。
本题另一个难点在于将二维转为一位数组。
考虑到第i行的状态由第i-1行和第i行递推过来,
所以能够利用滚动数组将二维压缩为一维数组,过程有点不太理解。留到以后慢慢想。
參考博文:http://blog.csdn.net/acm_davidcn/article/details/5887401
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int dp[1000010];
int maxn[1000010];
int num[1000010];
int main()
{
int M,N;
while(~scanf("%d%d",&M,&N))
{
dp[0] = maxn[0] = 0;
for(int i = 1; i <= N; i++)
{
scanf("%d",&num[i]);
dp[i] = maxn[i] = 0;
}
int MAXN;
for(int i = 1; i <= M; i++)//分为i段
{
MAXN = -0xffffff0;
for(int j = i; j <= N; j++)//第j个数字
{
dp[j] = max(dp[j-1]+num[j],maxn[j-1]+num[j]);
maxn[j-1] = MAXN;
MAXN = max(MAXN,dp[j]);
}
}
printf("%d\n",MAXN);
} return 0;
}
HDU1024_Max Sum Plus Plus【滚动数组】的更多相关文章
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus
A - Max Sum Plus Plus https://vjudge.net/contest/68966#problem/A http://www.cnblogs.com/kuangbin/arc ...
- HDU1024 Max Sum Plus Plus —— DP + 滚动数组
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...
- HUD 1024 Max Sum Plus Plus (滚动数组)
题意:从一个序列中选出分成不交叉的m段 的最大和 解析 : 题目中 1 <= n <=1000000 所以二维数组是不能用了 所以 要想到简化为一维 dp[i][j]表示以i结尾的前i个 ...
- HDU 1024 A - Max Sum Plus Plus DP + 滚动数组
http://acm.hdu.edu.cn/showproblem.php?pid=1024 刚开始的时候没看懂题目,以为一定要把那n个数字分成m对,然后求m对中和值最大的那对 但是不是,题目说的只是 ...
- HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化
题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...
- HDU - 1024 Max Sum Plus Plus 最大m段子段和+滚动数组优化
给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. dp[i][j]从前j个数字中选择i段,然后根据 ...
- Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)
题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
随机推荐
- C#图片辅助类,形成缩略图
完善一下别人的方法,成自己好用的工具 using System.Drawing; using System.Drawing.Imaging; namespace GXNUQzzx.Tools.Util ...
- linux php全能环境一键安装,小白福利!
phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 phpStudy for Linux 支持Apache/Ng ...
- Hive扩展功能(七)--Hive On Spark
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- Discuz! G变量的使用方法
1,G变量的使用方法: 例如:$_G['style'][boardlogo] 风格变量篇 $_G['style'] => Array (官方模板区 cr180整理 $_G['style'][st ...
- tidyverse生态链
一套完整的数据分析流程 , 如下图所示 从图中可以看到,整个流程包括读取数据,整洁数据,数据探索和交流部分.经过前两部分, 我们可以得到一个整理好的数据,它的每一行都是一个样本 , 每一列是一个变量. ...
- Redis 之持久化(rdb、aof)
Redis的持久化有2种方式 1快照 2是日志 测试aof:
- .net core 使用 textSharp生成pdf
引入Nuget包 using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.IO; namespace ...
- map集合遍历,放入id
背景,需要从电脑导入excel表格到网页上然后表格中公司需要对应数据库的id 通过key-value方法来对应id Office office = new Office();office.setG00 ...
- Scrapy爬虫框架示意图汇总
- 00.pip安装包
pip安装更换镜像源 pip install 包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com pip导出和导入 ...