hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41885 Accepted Submission(s): 15095
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. ^_^
Process to the end of file.
TLE的代码
#include<iostream>
#include<math.h>
#define ll long long
using namespace std;
ll dp[][],a[];//dp[i][j]表示将数组a中前j个数分成 i组的最大和
int main()
{
ll n,m;
while(~scanf("%lld%lld",&m,&n))
{
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=m;i++)
{
for(int j=i;j<=n;j++)
{
ll temp=-;
for(int x=i-;x<=j-;x++)
{
temp=dp[i-][x]>temp?dp[i-][x]:temp;
}
dp[i][j]=dp[i][j-]+a[j]>temp+a[j]?dp[i][j-]+a[j]:temp+a[j];
} }
cout<<dp[m][n]<<endl;
}
return ;
}
滚动DP优化
对动态转移方程:dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][x]+a[j]),i-1<=x<=j-1,dp[i-1][x]表示把数组a的前x个数分成i-1个子段的和的最大值。通过转移方程我们可以看出,对于求下一个dp[i][j]我们
只用到它的前两个状态dp[i][j-1]和dp[i-1][x],dp[i][j-1]在上一层循环中已经求出来了,因此我们只要再开一个滚动数组mx[j]来取代dp[i-1][x],随着j的改变不断更新mx数组就可以降低一层循环。
这样动态转移方程就变为:dp[i][j]=max(dp[i][j-1]+a[j],mx[j-1]+a[j]);
滚动数组mx[j]表示把数组a的前x个数分成i-1个子段的和的最大值
#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll dp[],mx[],a[];
//dp[j]是将数组前j个数分成i组的最大和,mx[j]是将数组a中前j个数分成任意组的最大和的最大值
ll max(ll a,ll b)
{
return a>b?a:b;
}
int main()
{
ll n,m,sum;
while(~scanf("%lld%lld",&m,&n))
{
memset(dp,,sizeof(dp));
memset(mx,,sizeof(mx));
for(int i=;i<=n;i++)
scanf("%lld",&a[i]); for(int i=;i<=m;i++)
{
sum=-;
for(int j=i;j<=n;j++)
{
dp[j]=max(dp[j-]+a[j],mx[j-]+a[j]);
mx[j-]=sum;//sum是将数组a的前j-1个数分成i组的最大和
sum=max(dp[j],sum);//更新sum,为下一次更新mx[j]准备
}
}
cout<<sum<<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)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...
- 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 ...
- HDU1024 Max Sum Plus Plus (优化线性dp)
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- HDU 1024 Max Sum Plus Plus【DP】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- HDU 1024 Max Sum Plus Plus(DP的简单优化)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1024:Max Sum Plus Plus(DP,最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- 懒人的ERP开发框架--2B&苦B程序员专用
在企业内部的ERP系统开发中,如果使用MS的技术,那么Winform + DevExpress + IIS + WCF +EF 就是懒人的黄金组合了,EF使用数据库优先,一般ERP应用主要关注点在数据 ...
- CAS实战の自定义注销
步骤一 在cas server端,设置/WebContent/WEB-INF/cas-servlet.xml: <bean id="logoutAction" class=& ...
- Git管理
在bitbucket用git用法 核心流程:从远端中心repo那里git clone到本地,再在本地开发(add,commit),通常会利用branch管理,如果觉得code没问题了,就push到远端 ...
- Sqler-Cmd
针对于sqler 工具cmd 部分 做了如下整理 1 Cluster Model 2 Regedit Model $servers= '192.168.25.xx','192.168.25.yy' W ...
- ajax 与 form 提交的区别
有如下几种区别: 1. Ajax在提交.请求.接收时,都是异步进行的,网页不需要刷新:Form提交则是新建一个页面,哪怕是提交给自己本身的页面,也是需要刷新的: 2. A在提交时,是在后台新建一个请求 ...
- eFrameWork学习笔记-eList
HTML: <div style="margin:8px;"> <h1>.不分页</h1> <asp:Repeater id=" ...
- 关于Unity中MonoBehaviour的构造函数
关于Unity中MonoBehaviour的构造函数 在学习Unity MVVM UI框架的时候,一不小给一个继承自MonoBehaviour类的子类编写了自定义构造函数,结果调Bug调了两个钟,特此 ...
- 使用CDI+制作支持半透明的Panle
创建一个自定义控件程序集,并修改父类为Panle,添加如下代码: public partial class OpaqueLayer : Panel { private Color transparen ...
- 如何关闭SQL进程
--通过下面的查询得到trace ID select * from sys.traces --修改下面的@traceid参数,关闭,删除对应的trace exec sp_trace_setstatus ...
- LOJ#6044. 「雅礼集训 2017 Day8」共(Prufer序列)
题面 传送门 题解 答案就是\(S(n-k,k)\times {n-1\choose k-1}\) 其中\(S(n,m)\)表示左边\(n\)个点,右边\(m\)个点的完全二分图的生成树个数,它的值为 ...