HDU1024 Max Sum Plus Plus (优化线性dp)
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. ^_^
InputEach 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.
OutputOutput 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.
别的博客在原理上说得很清楚了,代码里的注释解释了一下是如何进行时空优化的。
#include <bits/stdc++.h>
#define INF 1<<30
using namespace std;
int a[];
int dp[]={};//dp[i,j]表示选取a[j]分成i段的最大子段和 dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j])(k:i-1~j-1)
int m,n;
int pre[]={};
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
int i,j;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int ans=;
for(i=;i<=m;i++)
{
ans=-INF;//因为所求的最大状态一定在i=m这一轮出现,所以每轮开始把ans设为负无穷没有影响
for(j=i;j<=n;j++)//从i开始,要不然的话少于i个数分不成i段了
{
dp[j]=max(dp[j-]+a[j],pre[j-]+a[j]);//在这里,一开始整个pre数组存储的本身就是i-1阶段里的各个最大值
pre[j-]=ans;//当第二重循环跑起来时 ,在更新dp数组的同时也在更新pre数组,这就是这一句的含义,而在这一重循环中更新pre数组对当前是没有影响的(因为已经越过去了)
ans=max(ans,dp[j]);//在开头初始化后,ans能保证一直是当前层的最大值
}
}
cout<<ans<<endl;
}
return ;
}
HDU1024 Max Sum Plus Plus (优化线性dp)的更多相关文章
- hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1024 Max Sum Plus Plus 【DP】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 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 ...
- hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...
- Max Sum (hdu 1003 简单DP水过)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU 1024 Max Sum Plus Plus(基础dp)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化
题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...
随机推荐
- Django_连接MySQL
1. 在Settings中修改 2. 创建数据库 3. 连接mysql 4. pymysql 4.1 安装pymysql 在项目的init文件中添加 Django2.2 不需要伪装
- noobSTL-1-配置器-1
noobSTL-1-配置器-1 1.要点分析 1.1 可能让你困惑的C++语法 组态 即配置. 临时对象 一种无名对象.有时候会刻意地制造临时对象. 静态常量整数成员在class内部直接初始化 con ...
- unidac 断线重连
unidac 断线重连 因为物理网络断连或数据库超时踢掉连接等原因,Tuniconnection建立的连接可能会断掉. 在默认状态下,程序会抛出异常框,并罢工. 其实Tuniconnection是可以 ...
- THINKPHP 模板上传图片--后台接收图片
模板 {extend name="public/base" /} {block name="body"} <div class="row&quo ...
- Cheapest Palindrome
这个区间dp解的话是先知道小区间再推大区间,具体需要分类讨论当小区间已经是回文串了,下一层判断,所以一层一个呢还是一层两个呢, 下面讨论一层一个的话是什么情况,那么如果一层两个,可以在评论区写下代码供 ...
- python如何将自己写的代码打包供他人使用
背景: 利用setuptools来实现,所以先安装setuptools,其实Python2.7.9及之后的版本都自带安装setuptools了,无需在另外安装 如果没有安装setuptools的直接下 ...
- Angular 使用 frame 加载网络资源显示路径不安全问题
Angular 使用 frame 加载网络资源显示路径不安全问题 做项目的时候,angular 使用 frame 加载网络pdf文件的时候出现 unsafe value 问题,路径不安全.解决办法. ...
- DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL 未能在“xmlhttpRequest”上执行“open”:无效的URL。
出现这个报错主要是baseurl:http://192.168.*.*/后面的(/)或是请求里面的url:/user/login中前面的(/)有一个漏掉了,导致合成的路径不完整,所以报错:无效的URL
- centos7的netca命令和netmgr命令、dbca命令等基础知识点
netca命令: netmgr命令: dbca命令: database assistant configuration 中的SGA 和PGA SGA:System Global Area是Oracl ...
- Redis注册成服务
注册服务 redis-server.exe –-service-install redis.windows.conf 删除服务 redis-server –-service-uninstall 开启服 ...