Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26012   Accepted: 8416

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 integersh1,...,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.
 
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e5+;
int a[maxn];
int s[maxn];
int w[maxn];
int top;
int main(){
int n;
while(scanf("%d",&n),n){
memset(a,,sizeof(a));
memset(s,,sizeof(s));
memset(w,,sizeof(w));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int p=;
long long ans=;
a[n+]=;
for(int i=;i<=n+;i++){
if(a[i]>s[p]){
s[++p]=a[i];
w[p]=;
}else{
int width=;
while(s[p]>a[i]){
width+=w[p];
ans=max(ans,(long long)width*s[p]);
p--;
}
s[++p]=a[i];
w[p]=width+;
}
}
cout<<ans<<endl;
}
}

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. ruby 第三方模块unirest使用

    Creating Requests require 'unirest' response = Unirest.post 'http://httpbin.org/post', headers:{ Acc ...

  2. 阿里云提醒 网站被WebShell木马后门的处理过程

    昨晚凌晨收到新客户的安全求助,说是阿里云短信提示,网站有webshell木马文件被植入,我们SINE安全公司立即成立,安全应急响应小组,客户提供了阿里云的账号密码,随即登陆阿里云进去查看到详情,登陆云 ...

  3. vue---day04

    1. Node.js 1.1 介绍: - Node.js 是一个JavaScript运行环境,实质上是对Chrome V8引擎的封装. - Node.js 不是一个 JavaScript 框架,不同于 ...

  4. ubuntu配置机器学习环境(四) 安装intel MKL

    在这一模块可以选择(ATLAS,MKL或者OpenBLAS),我这里使用MKL,首先下载并安装英特尔® 数学内核库 Linux* 版MKL,下载链接, 请下载Student版,先申请,然后会立马收到一 ...

  5. Nginx一直报504超时,配置相关参数好了

    相关参数:large_client_header_buffers 4 16k;client_max_body_size 30m;client_body_buffer_size 128k;proxy_c ...

  6. P1823 音乐会的等待(单调栈)

    P1823 音乐会的等待 题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么 ...

  7. Android StateListDrawable的坑

    有问题的代码: StateListDrawable background = new StateListDrawable(); CircleDrawable pressedDrawable = new ...

  8. IAR工程名修改

    修改.dep..ewd..ewp..eww四个文件的文件名 删除.ewt文件(如果存在) 记事本打开.eww文件,修改<path></path>间的.ewp文件名 打开工程,打 ...

  9. OrCAD设置原理图页面大小

    1. 右键要修改的原理图文件 2. 选择适合的尺寸

  10. vue之vue-cookies使用

    一.安装vue-cookies npm install --save vue-cookies 或者 yarn add vue-cookies 二.引入vue-cookie // 方式一:require ...