Largest Rectangle in a Histogram (最大子矩阵)
hdu 1506
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
InputThe input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.OutputFor each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
// 第一个数 n ,代表后面有几个数,后面 n 个数代表长方体的高,宽都是 1 ,并且n个长方体紧靠,问最大的矩形面积
L[i] 代表 h[j]>=h[i](j<=i) 的最远的编号
R[i] 代表 h[j]>=h[i](j>=i) 的最远的编号
求出来后,就可以去找最大矩形了,遍历一遍 maxS = max( (R[i]-L[i]+1)*h[i] ) (1<=i<=n)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 100005
#define LL long long
LL L[MAXN];
LL R[MAXN];
LL h[MAXN]; int main()
{
int n;
while (scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++)
scanf("%I64d",&h[i]);
h[]=h[n+]=-;
for (int i=;i<=n;i++)
{
L[i]=i;
while (h[L[i]-]>=h[i]) //利用 L[] 去循环,其实很快
L[i]=L[L[i]-];
}
for (int i=n;i>=;i--)
{
R[i]=i;
while (h[R[i]+]>=h[i])
R[i]=R[R[i]+];
}
LL ans = ;
for (int i=;i<=n;i++)
{
LL area = (R[i]-L[i]+)*h[i];
if(area>ans) ans = area;
}
printf("%I64d\n",ans);
}
return ;
}
Largest Rectangle in a Histogram (最大子矩阵)的更多相关文章
- HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- DP专题训练之HDU 1506 Largest Rectangle in a Histogram
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- Largest Rectangle in a Histogram(HDU1506)
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
随机推荐
- 路由器漏洞复现分析第三弹:DVRF INTRO题目分析
这个项目的目的是来帮助人们学习X86_64之外其他架构环境,同时还帮助人们探索路由器固件里面的奥秘. 本文通过练习DVRF 中INTRO 部分的题目来学习下MIPS 结构下的各种内存攻击. DVRF: ...
- swftools使用
为了支持gif转swf以及pdf转swf.编译swftools过程中遇见几个问题,记录一下. 首先下载swftools:http://www.swftools.org/ 它依赖几个包,这里我使用的版本 ...
- 分布式服务自增长唯一ID小结
1.常用生成唯一ID的方式,例如UUID 2.生成唯一自自增长ID方式: 例如: Zookeeper的增加ID: redis的incr方法 mongodb的objectId 3.采用雪花模型 如下代码 ...
- 转: 阿里跨平台移动开发工具Weex
对于移动开发者来说,Weex主要解决了频繁发版和多端研发两大痛点,同时解决了前端语言性能差和显示效果受限的问题.开发者可通过Weex官网申请内测.(http://alibaba.github.io/w ...
- 关于批量插入数据之我见(100万级别的数据,mysql)
因前段时间去面试,问到怎样高效向数据库插入10万条记录,之前没处理过类似问题.也没看过相关资料,结果没答上来,今天就查了些资料.总结出三种方法: 測试数据库为mysql!!! 方法一: public ...
- IOS 开发环境,证书和授权文件是什么?
一.成员介绍 1. Certification(证书) 证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种: 1) Developer Certification(开发证书) 安装 ...
- 文件压缩和解压 FileStream GZipStream
using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...
- MySql修改root密码、设置IP访问
先登录:mysql -h 192.168.5.116 -P 3306 -u root -p123456 首次登陆无密码命令:mysql -h 192.168.5.116 -P 3306 -u root ...
- .Net Framework 与 SQL Server 2005 混乱的时间最大最小值
Net Framewrok 中,DateTime.MinValue => 0001/01/01 00:00:00SqlDateTime.MinValue.Value => 1753/01 ...
- 微软“小冰”识狗与人工神经网络(I)
2014年8月21日,微软"小冰"网络机器人推出了一项图像识别技能:"小冰识狗". "小冰"怎么会"识狗"呢? 依据微软 ...