[POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法
最大子段和
Ο(n) 的时间求出价值最大的子段
#include<cstdio>
#include<iostream>
using namespace std;
int n,maxn;
],ans[];
int main(){
scanf("%d",&n);
;i<=n;i++){
scanf("%d",&val[i]);
ans[i]=max(ans[i-]+val[i],val[i]);
maxn=max(maxn,ans[i]);
}
printf("%d",maxn);
;
}
运用了dp 的思想,ans[i] 表示 i之前的包含 i 的连续子段的最大值,因为包含 i ,所以可以直接从 ans[i-1] 转移。
最大矩阵和
可以用 O(n2) 的时间枚举开始和结束的行,然后一个前缀和数组存每列这两行之间的权值和,就转变为最大子段和问题了,总时间复杂度 O(n3) 。
顺便上 POJ1050 代码(基本是裸题)
#include<cstdio>
#include<iostream>
using namespace std;
int n,maxn;
],ans[];
][];
][];
int main(){
scanf("%d",&n);
;i<=n;i++){
;j<=n;j++)
scanf(][j]+val[i][j];
}
;i<=n;i++){
for(int j=i;j<=n;j++){
;
;p<=n;p++){
heng[p]=qzh[j][p]-qzh[i-][p];
ans[p]=max(heng[p],ans[p-]+heng[p]);
maxx=max(maxx,ans[p]);
}
maxn=max(maxn,maxx);
}
}
printf("%d",maxn);
;
}
[POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法的更多相关文章
- [POJ1050]To the Max
[POJ1050]To the Max 试题描述 Given a two-dimensional array of positive and negative integers, a sub-rect ...
- (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54338 Accepted: 28752 Desc ...
- [POJ1050]To the Max(最大子段和)
题目链接 http://poj.org/problem?id=1050 题意 求最大子矩阵和. 题解 即求二维的最大子段和.二维数组sumRec[I][j]存储原始数组数据rec[0][j] to r ...
- [POJ1050]To the Max (矩阵,最大连续子序列和)
数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...
- POJ1050 To the Max 最大子矩阵
POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...
- poj1050 To the Max(降维dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49351 Accepted: 26142 Desc ...
- [POJ1050]To the Max(最大子矩阵,DP)
题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...
- hdu1003 Max Sum---最大子段和+记录开始结束点
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求最大子段和,并且输出最大子段和的起始位置和终止位置. 思路: 根据最大子段和基本 ...
- 51nod 1049 1049 最大子段和 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 令 dp[i]表示为以a[i]结尾的最大子段和,则 dp[i]= ...
随机推荐
- CentOS7网卡命名规则
CentOS6之前基于传统的命名方式如:eth1,eth0.... Centos7提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名是全自动的.可预知的,缺点是比eth0. ...
- css入门二-常用样式
css入门二-常用样式总结 基本标签样式 背景色background-color 高度height; 宽度width; 边框对齐以及详细设定举例 width/*宽度*/: 80%; height/*高 ...
- NLP+句法结构(三)︱中文句法结构(CIPS2016、依存句法、文法)
摘录自:CIPS2016 中文信息处理报告<第一章 词法和句法分析研究进展.现状及趋势>P8 -P11 CIPS2016> 中文信息处理报告下载链接:http://cips-uplo ...
- linux驱动---用I/O命令访问PCI总线设备配置空间
PCI总线推出以来,以其独有的特性受到众多厂商的青睐,已经成为计算机扩展总线的主流.目前,国内的许多技术人员已经具备开发PCI总线接口设备的能 力.但是PCI总线的编程技术,也就是对PCI总线设备的操 ...
- fastboot烧写hi3531
Boot Downloading started. Boot 100 % Downloaded. Boot Downloading completed! U-Boot 2010.06 (Jan 04 ...
- javax.el.PropertyNotFoundException:Property 'statisDate' not found on type java.lang.String
1.错误描述 javax.el.PropertyNotFoundException:Property 'statisDate' not found on type java.lang.String 2 ...
- Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- 求助:关于sql如何统计时间的问题
三.现在我们假设应用计时分为app应用和web应用,需要考虑如下几个方面: (1)多时间段(2)表中有冗杂数据 (3)用户是在web端和app端都登陆,这种类型的重复时间段只能取其一 存在数据: 存在 ...
- css补充
(一)水平对齐1.使用margin属性水平对齐可通过将左和右外边距设置为 "auto",来对齐块元素.除非已经声明了 !DOCTYPE,否则使用 margin:auto 在 IE8 ...
- 「拆小鹤」使用 python 实现 QQ机器人服务。
使用的是python的qqbot机器人库,我其实只是实现了这个库的一个插件. 具体的说明,我觉得qqbot的官方文档,还有我的插件的注释都写得很详细了,可以直接看.所以有空再写吧. 没错我就是懒..