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. ^_^

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)的更多相关文章

  1. hdu1024 Max Sum Plus Plus 滚动dp

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. HDU1024 Max Sum Plus Plus 【DP】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. 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 ...

  5. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  6. Max Sum (hdu 1003 简单DP水过)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. 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 ...

  8. HDU1081:To The Max(最大子矩阵,线性DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...

  9. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

随机推荐

  1. 初学Java时使用记事本开发出现的中文乱码问题

    如果是使用微软记事本,那就会麻烦一点,将后缀改为.txt,用记事本打开,左上方:文件→另存为→弹出的窗口的下方会出现使用的编码形式,将编码形式修改为ANSI编码,点击保存→确定替换即可,修改后将后缀改 ...

  2. ClassLoad

    是什么 用来加载 Class 的.它负责将 Class 的字节码形式转换成内存形式的 Class 对象.字节码可以来自于磁盘文件 .class,也可以是 jar 包里的 .class,也可以来自远程服 ...

  3. nginx 定义:响应头和请求头

    1) 响应头 add_header 例如: add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; a ...

  4. 配置yum仓库:yum install 软件

    1.一个重要模板: 进入/etc/yum.repos.d文件夹,新建一个xiaoxu.repo文件,其中xiaoxu可以根据需要来取名. [模板] vim  xiaoxu.repo [rhel]    ...

  5. Attribute "resultType" must be declared for element type "update" or "insert"

    仔细查看错误如图所示: 解决错误就是把resultType去掉,因为在insert和update语句中是没有返回值的.小坑小坑 转自:https://blog.csdn.net/u013144287/ ...

  6. c#常用控件及简写

    c#常用控件及简写

  7. EntityFramework 根据时间筛选数据

    需求:根据当前时间,获取条件合适的数据,其中截止时间只比较日期. 1. 运行会报错的版本: var lifeWorkEventBatch = clientRepositoryContainer.Lif ...

  8. Saber-图集

    PS:狙击手

  9. cookie、session以及中间件

    cookie cookie是保存客户端浏览器上的键值对,是服务端设置在客户端浏览器上的键值对,也就意味着浏览器其实可以拒绝服务端的'命令',默认情况下浏览器都是直接让服务端设置键值对 设置cookie ...

  10. 吴裕雄 python 机器学习——数据预处理标准化StandardScaler模型

    from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...