Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29498   Accepted: 9539

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 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.

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.
 
题意:有N个矩形,宽度都为1,给出N个矩形的高度,求由这N个矩形组成的图形包含的最大的矩形面积
 
//单调递增栈
#include<iostream>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,s,top;
int main()
{
while(~scanf("%lld",&n))
{
if(n==)
break;
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=-;//为找比a[n-1]小的数准备,因为是递增栈,将a[n]设为最小值
s=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]>=a[p.top()])//看题目要求是否要严格单调递增,这里只要求递增
p.push(i);
else
{
while(!p.empty()&&a[i]<a[p.top()])//找到第一个小于栈顶元素的数的下标
{
top=p.top();
p.pop();//只在在出栈的过程中以a[top]为最小值更新面积s
if(s<(i-top)*a[top])
s=(i-top)*a[top];
}
p.push(top);//只将延伸到最左端的元素入栈,并且以最左端的元素的!坐标!为起点,找下一个比a[i]大的最长增区间
a[top]=a[i];//修改该位置的值为a[i] }
}
printf("%lld\n",s);
}
return ; }

po'j2559 Largest Rectangle in a Histogram 单调栈(递增)的更多相关文章

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

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

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

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

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

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

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

                                                                                                       L ...

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

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

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

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

  7. POJ2559 Largest Rectangle in a Histogram 单调栈

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

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

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

  9. 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. centos下修改文件后如何保存退出

    centos下修改文件后如何保存退出 保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :w ...

  2. C# Redis Server分布式缓存编程(二)(转)

    出处;http://www.cnblogs.com/davidgu/p/3263485.html 在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Red ...

  3. UVa 11419 SAM I AM (最小覆盖数)

    题意:给定一个 n * m 的矩阵,有一些格子有目标,每次可以消灭一行或者一列,问你最少要几次才能完成. 析:把 行看成 X,把列看成是 Y,每个目标都连一条线,那么就是一个二分图的最小覆盖数,这个答 ...

  4. (深搜)棋盘问题 -- poj -- 1321

    链接: http://poj.org/problem?id=1321 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2889 ...

  5. 两种步骤 更改 EBS R12界面LOGO以及内容

    from:metalink more: Note 174219.1 - How To Change The Logo In The Oracle Application Menu Note 84975 ...

  6. [idea]对于前端里面一些事情的看法

    一些是自己的想法,另一些是看博客或者书籍里面得出的,随手记在这里. 基于页面的开发 最初的前端资源模式是基于页面的,像最开始接触web.开发web时候,一般是新建页面-引入jQuery-新建index ...

  7. LINQ to Entities 查询中的标准查询运算符

    投影和筛选方法 投影指的是转换的结果集到所需的窗体中的元素. 例如,可以从结果集中的每个对象投影所需的属性子集,可以投影一个属性并对其执行数学计算,也可以从结果集投影整个对象. 投影方法有 Selec ...

  8. sql 循环 ,随机数,循环插入一年数据

    --循环插入一年的时间 DROP TABLE #temp CREATE TABLE #temp (date DATETIME); DECLARE @year INT ; WITH a1 AS ( SE ...

  9. Fiddler关闭后打不开网页

    今天项目系统测试的时候,CS客户端的Restful请求都失败,但是实际上的服务是正常开启的,马上通过cmd指令ping了一下服务,正常:再用telnet试了一下端口,也是正常.不过随后发现在这台电脑上 ...

  10. iOS 内嵌 View 的响应

    遇到一个问题就是我有一个 UITextField,点击后不能编辑而是会显示一个自定义的 dialog. 但发现问题是,UITextField 的对点击事件的反应非常吃顿,有时候好使有时候不好使. 后来 ...