Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19782   Accepted: 6393

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.
 
/*/
大二这学期开学真是忙爆了。作为一个班干部,天哪。。 好久没有刷题了,想起以前听了岛娘的一节课,单调栈,好像有点似懂非懂,于是抽时间看了一下,总算是搞通了。。。 将读入的数据一个个压栈,奖数据与栈顶进行比较大小,如果这个数比栈顶小,就计算以站顶为高的最大矩形的大小,弹出栈顶。如果这个数比栈顶大,直接就压进栈。这样一系列操作之后,就会发现栈里面剩下一个递增数列,用一个pair来保存此时栈高度和前面达到这个高度的个数,然后按照前面的思想去计算每一个高度最大矩形面积是多大。

AC代码:
/*/
#include "stdio.h"
#include "string.h"
#include "stack"
#include "algorithm"
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MX = 1e5 + 5;
int main() {
LL n,h;
while(~scanf("%lld",&n)) {
          if(n==0)break;
stack<PII> Q;
while(!Q.empty()) {
Q.pop();
}
LL ans=0;
for(LL i=0; i<n; i++) {
scanf("%lld",&h);
LL Now_Big_W=0;
while(!Q.empty() && Q.top().first >= h ) {
LL H = Q.top().first;
LL W = Q.top().second;
Q.pop();
Now_Big_W+=W;
ans=max(ans,H*Now_Big_W);
}
Q.push(PII(h,Now_Big_W+1));
}
LL The_number_W=0;
while(!Q.empty()){
The_number_W+=Q.top().second;
ans=max(ans,Q.top().first*The_number_W);
Q.pop();
}
printf("%lld\n",ans);
}
return 0;
}

  

  

poj 2559 Largest Rectangle in a Histogram - 单调栈的更多相关文章

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

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

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

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

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

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

  4. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  5. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  6. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  7. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

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

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

  9. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

随机推荐

  1. sss

    function handleTouchEvent(event) {    if (event.touches.length == 1) {        var output = document. ...

  2. PYTHON 内置函数

    bin() #二进制 r = bin(11) print(r) # 0b1011 oct() #八进制 r = oct(14) print(r) #0o16 int() #十进制 r = int(10 ...

  3. 将本地项目提交到coding上托管

    1:   注册coding并新建项目test2:在终端 cd 到要提交的项目  使用git init创建.git文件夹3:使用git pull  <项目地址>https的那个4:git a ...

  4. ORACLE LINUX 6.3 + ORACLE 11.2.0.3 RAC + VBOX安装文档

    ORACLE LINUX 6.3 + ORACLE 11.2.0.3 RAC + VBOX安装文档 2015-10-21 12:51 525人阅读 评论(0) 收藏 举报  分类: Oracle RA ...

  5. NSIS对话框单位造成的控件移位问题

    在使用NSIS脚本开发安装卸载程序,使用自定义的nsdialog控件.发现在小部分系统上安装时,一些控件会消失,或者挪位.于是排除问题,看看这些控件的为位置和坐标,发现基本上是使用了对话框单位的控件, ...

  6. C#操作XML总结

    1.using System.Xml; using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xm ...

  7. Python-第三方库requests详解

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  8. 还敢说你是程序员?一律师闲着没事写了个app,用户量600万

    今天周五,是我在上海上班的第五天. 这几天怎么说呢,跟混日子差不多,因为处处有“”惊喜”. 上班第一天领来办公电脑,登上自己的公司邮箱,惊喜来了!我的擦擦擦,全TM是英文呐!作为一个从村儿里来的码农, ...

  9. 11. KVC And KVO

    1. KVC And KVO  的认识 KVC/KVO是观察者模式的一种实现  KVC全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用 ...

  10. Java数据类型和MySql数据类型对应表