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. pip install Yellowfin失败的问题

    上面的方法简单有效 https://blog.csdn.net/quqiaoluo5620/article/details/80608474

  2. 数字IC设计入门书单

    首发于观芯志 写文章     数字IC设计入门书单 Forever snow   1 年前 作者:Forever snow链接:你所在领域的入门书单? - 知乎用户的回答来源:知乎著作权归作者所有,转 ...

  3. EZ 2017 12 17初二初三第一次膜你赛

    以后平时练习还是写一写吧. (题目搞来搞去太烦了,直接PDF存起来) T1 水题(???),主要是数据水,正解是设一个阙值,然而根本没人打.(暴力出奇迹) CODE #include<cstdi ...

  4. Redis学习之路(四)之Redis集群

    [toc] #Redis集群 1.Redis Cluster简介 Redis Cluster为Redis官方提供的一种分布式集群解决方案.它支持在线节点增加和减少. 集群中的节点角色可能是主,也可能是 ...

  5. shell脚本事例 -- 获取当前日期的前一天日期

    记录一个shell脚本事例,事例中包括shell的一些语法(函数定义.表达式运算.if.case...) #!/bin/sh #获取当前时间 RUN_TIME=`date +%H%M%S` #取当前日 ...

  6. 您需要来自XXX的权限才能对此文件夹进行更改

    解决办法: cmd命令:del/f/s/q 文件夹

  7. 本地mysql快速迁移到服务器数据库中

    我们可以使用linux的scp命令(scp无法在windows使用),加上mysql自带的mysqldump,能很快的完成数据库的迁移 将本地的数据库(music_db)导出为sql文件(music_ ...

  8. 利用Kinect实现用指尖隔空控制鼠标(源码放出)

    简介 此程序为利用Kinect实现用手指隔空控制鼠标,是我另一个项目的一部分,因为在另外那个项目中鼠标的click是通过一种特殊的方式实现的,因此这个程序只实现了用手控制鼠标的移动,并没有点击的功能. ...

  9. Asp.Net_from标签中的Enctype=multipart/form-data作用

    ENCTYPE="multipart/form-data"用于表单里有图片上传. <form name="userInfo" method="p ...

  10. Js_checkbox的互斥

    function ck_click(obj) { var ck_20 = document.getElementById("ck_20"); var ck_25 = documen ...