hdu 1024 最大M子段dp
题目:
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27299 Accepted Submission(s):
9499
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.
^_^
followed by n integers S1, S2,
S3 ... Sn.
Process to the end of
file.
line.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
#include<bits/stdc++.h>
using namespace std;
const int inf=999999999;
int dp[1000005],a[1000005],pre[1000005];
int main()
{
int n,m,i,j,k,t;
while (cin>>m>>n){int maxn=-inf;t=0;
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (i=1;i<=m;i++){
maxn=-inf; //每一轮开始都将maxn初始化
for (j=i;j<=n;j++){
dp[j]=max(pre[j-1],dp[j-1])+a[j]; //先使用在更新上一轮的值
pre[j-1]=maxn; //此处很重要,不可写成下文注释的形式,因为一旦那样写下一个j计算时用到的j-1变成了本轮的最大值的意思,概念就变了
if (maxn<dp[j]) maxn=dp[j];
//pre[j]=maxn;
}
//for(int l=0;l<=n;l++) cout<<pre[l]<<" ";
//cout<<endl;
}
cout<<maxn<<endl;
}
return 0;
}
hdu 1024 最大M子段dp的更多相关文章
- hdu 1024 最大m子段和
注:摘的老师写的 最大m子段和问题 以-1 4 -2 3 -2 3为例最大子段和是:6最大2子段和是:4+(3-2+3)=8所以,最大子段和和最大m子段和不一样,不能用比如先求一个最大子段和,从序列中 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 怒刷DP之 HDU 1024
Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Max Sum Plus Plus HDU - 1024
Max Sum Plus Plus HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...
随机推荐
- php header utf8 插入header("Content-type: text/html; charset=utf-8");
PHP文件插入header("Content-type: text/html; charset=utf-8"); 相当于页面里面的<meta http-equiv=" ...
- pdf2swf 中文乱码问题
准备资料: 1.xpdfbin-win-3.03.zip.xpdf-chinese-simplified.tar.gz 下载地址:http://www.foolabs.com/xpdf/downloa ...
- laravel 对接支付宝
使用的库 omnipay-alipay 申请支付宝支付 这个就不说了, 不明白如何下手的伙伴让运营去和支付宝客服联系吧 composer 安装git库 将以下代码添加到 composer.json { ...
- html模板生成静态页面及模板分页处理
它只让你修改页面的某一部分,当然这"某一部分"是由你来确定的.美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种 ...
- lambda表达式学习
Lambda 表达式是一种可用于创建 委托 或 表达式目录树 类型的 匿名函数 . 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 ...
- |和||以及&和&&
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx Logical OR Operator 按位或 This operator has hig ...
- 《重构网络:SDN架构与实现》Chapter7 SDN与网络虚拟化 随笔
参考: <重构网络:SDN架构与实现>,作者:杨泽卫.李呈. Chapter7 SDN与网络虚拟化 结构 7.1 网络虚拟化 7.1.1 为什么需要网络虚拟化技术 7.1.2 网络虚拟化 ...
- 【Coursera】Technology :Fifth Week(1)
Store and Forward Networking 在长途连接中,路由的数量 取决于 最优路径(花费最少).也就是说被地理条件所限制. 所以 Store and Forward Networki ...
- QQ名片自动点赞
2017-01-23 简介:QQ名片自动点赞,1秒左右可点完1屏好友的赞,每个好友10个赞. 尺寸: 720*1280 DPI:320 宿主:3.1.2.10711 系统:Android v5.11 ...
- UVa 1626 括号序列(矩阵连乘)
https://vjudge.net/problem/UVA-1626 题意: 输入一个由 "(" . ")" . "[" . " ...