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 ...
随机推荐
- 线程池(ExecutorService)初体验
背景:查询月统计数据,因为查询日统计数据功能已经实现.月统计数据,只是参一个List(date) 参数,for循环调用日统计,然后把结果整合就OK. 问题:单线程跑 太耗时间 解决方案:使用多线程, ...
- 下载图片(vue 下载图片)
downloadImg(){ const url = this.imgUrl // window.open(_this.detail.imgUrl) let xmlhttp=new XMLHttpRe ...
- springboot引入Oracle依赖
最近学习spring boot,在网上找一些项目学习,有的项目引入了oracle驱动包,自己搭建一直不成功,百度发现说是权限问题无法下载. 然后参考下面博客终于解决:springboot引入Oracl ...
- redis持久化优缺点
- pycharm pro与你同在
下载激活码和激活依赖的jar包,地址(https://www.lanzous.com/b00t4aneb密码:67t9)按照步骤操作即可第一步:正确安装 Pycharm 软件(版本2019.1.3 p ...
- servlet中Request与response使用
服务器根据请求自动创建传入HttpServletRequest对象和HttpServletResponse对象 @Override protected void service(HttpServlet ...
- cookie、session以及中间件
cookie cookie是保存客户端浏览器上的键值对,是服务端设置在客户端浏览器上的键值对,也就意味着浏览器其实可以拒绝服务端的'命令',默认情况下浏览器都是直接让服务端设置键值对 设置cookie ...
- Stylus-import
Stylus Import Disclaimer: In all places the @import is used with Stylus sheets, the @require could b ...
- vue入门基础知识点测试
vue入门基础知识点测试 1.文本(值绑定){{var}}----控制<div></div>的值显示当前时间,且1秒更新一次.(可查阅 setinterval 函数,时间Dat ...
- 每天进步一点点------Sobel算子(2)
转载 http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值. ...