Description

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.

Input

The 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 that1<=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.

Output

For 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

Hint

Huge input, scanf is recommended.

Source

Ulm Local 2003
 
 
题目大意:

这题就是个单调栈。贴一下书上的题解吧:

代码:

 program rrr(input,output);
var
q:array[..]of longint;
a,l,r:array[..]of int64;
n,i,t:longint;
ans:int64;
function max(a,b:int64):int64;
begin
if a>b then exit(a) else exit(b);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
while true do
begin
read(n);if n= then break;
for i:= to n do read(a[i]);
t:=;q[]:=;
for i:= to n do
begin
while (t>) and (a[i]<=a[q[t]]) do dec(t);
l[i]:=q[t];
inc(t);q[t]:=i;
end;
t:=;q[]:=n+;
for i:=n downto do
begin
while (t>) and (a[i]<=a[q[t]]) do dec(t);
r[i]:=q[t];
inc(t);q[t]:=i;
end;
ans:=;
for i:= to n do ans:=max(ans,a[i]*(r[i]-l[i]-));
writeln(ans);
end;
close(input);close(output);
end.

poj2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章

  1. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

  2. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  3. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  6. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  7. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498 ...

  8. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  9. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  10. Largest Rectangle in a Histogram /// 单调栈 oj23906

    题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...

随机推荐

  1. c# 淘宝运单查询

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. 20155229《网络对抗技术》Exp2:后门原理与实践

    实验预习 后门: 指绕过安全控制而获取对程序或系统访问权的方法.最主要目的就是方便以后再次秘密进入或者控制系统. 木马与后门的区别: 木马:通过欺骗用户的方法(包含捆绑,利用网页等)让用户不知不觉的安 ...

  3. 20155338《网络对抗》 Exp4 恶意代码分析

    20155338<网络对抗>恶意代码分析 实验过程 1.计划任务监控 在C盘根目录下建立一个netstatlog.bat文件(先把后缀设为txt,保存好内容后记得把后缀改为bat),内容如 ...

  4. 字典学习(Dictionary Learning, KSVD)详解

    注:字典学习也是一种数据降维的方法,这里我用到SVD的知识,对SVD不太理解的地方,可以看看这篇博客:<SVD(奇异值分解)小结 >. 1.字典学习思想 字典学习的思想应该源来实际生活中的 ...

  5. 《图说VR入门》——360全景视频

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/53674647 作者:car ...

  6. idea java方法中 传多个参数对象 的复制粘贴快速处理方法

    比如像这种的传多个参数对象,我是直接复制过来,然后把第一个字母改成大写,然后后面的实例对象敲一个第一个字符的小写,回车就直接出来了 在写调用参数的地方,ctrl+p 调出提示,然后按下提示里的实例的第 ...

  7. stl源码剖析 详细学习笔记 算法(2)

    //---------------------------15/03/29---------------------------- //****************************set相 ...

  8. Beyond Compare文本对比中提示编辑禁止的解决方法

    Beyond Compare是一款拥有文本比较功能的智能化软件,它支持在文本比较的同时,直接对差异文本进行修改.删除.编辑等一系列操作,这样一来,节约了文本对比的时间.但是在使用Beyond Comp ...

  9. EntityFramework Core 2.x (ef core) 在迁移中自动生成数据库表和列说明

    在项目开发中有没有用过拼音首字母做列名或者接手这样的项目? 看见xmspsqb(项目审批申请表)这种表名时是否有一种无法抑制的想肛了取名的老兄的冲动? 更坑爹的是这种数据库没有文档(或者文档老旧不堪早 ...

  10. CentOS 6.8 安装Maven

    1.下载maven安装包到developer文件目录下 2,解压缩maven,命令: -bin.tar.gz 3.配置maven路径到环境变量中 export JAVA_HOME=/usr/java/ ...