Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26549   Accepted: 8581

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

单调栈入门题...


#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
int a[100005];
int st[100005];
int L[100005];
int R[100005];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLIN
while(1)
{
scanf("%d",&n);
if(n==0)break;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
stack<int>st;
for(int i=1;i<=n;i++)
{
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty()){L[i]=1;st.push(i);continue;}
L[i]=st.top()+1;
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--)
{
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty()){R[i]=n;st.push(i);continue;}
R[i]=st.top()-1;
st.push(i);
} ll ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,1ll*a[i]*(R[i]-L[i]+1));
}
printf("%lld\n",ans);
}
return 0;
}

poj 2559 Largest Rectangle(单调栈)的更多相关文章

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

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

  2. poj 2559 Largest Rectangle in a Histogram 栈

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

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

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

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

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

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

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

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

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

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

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

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

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

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

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

随机推荐

  1. PTA(Basic Level)1030.完美数列

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤*m**p*,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. ...

  2. Go语言中的切片(十)

    go中数组的长度是固定的,且不同长度的数组是不同类型,这样的限制带来不少局限性.于是切片就来了,切片(Slice)是一个拥有相同类型元素的可变长度的序列.它是基于数组类型做的一层封装.它非常灵活,支持 ...

  3. 关于springcloud hystrix 执行 hystrix.stream 跳转失败的问题

    经过观看网友的总结:应该时版本的问题.某些版本没有对/hystrix.stream进行配置 所以解决方案(网友答案): 需要配置类配置下面 @Bean public ServletRegistrati ...

  4. python中format格式化函数

    http://www.runoob.com/python/att-string-format.html

  5. Java Web开发技术教程入门-数据库

    补更:阅战阅勇第六天 今天阅读了这本书的第六章-访问数据.首先,这本书讲解的是MySql数据库,它是一个关系型数据库管理系统,是由瑞典MySqlAB公司开发,目前属于Oracle旗下公司.在web应用 ...

  6. computed、watch、methods的区别

    computed:计算属性是用来声明式的描述一个值依赖了其它的值.当你在模板里把数据绑定到一个计算属性上时,Vue 会在其依赖的任何值导致该计算属性改变时更新 DOM.这个功能非常强大,它可以让你的代 ...

  7. zabbix mongodb 监控添加

    在zabbix 上添加mongodb的监控 由于使用的是zabbix 3.0 所有在模板里面又自己的模板名字叫:Template MongoDB 所以 客户端的配置如下 到配置文件目录 /usr/lo ...

  8. linux上搭建单机版hadoop和spark

    依赖的安装包 首先hadoop和spark肯定是必须的,而hadoop是用java编写的,spark是由Scala编写的,所以还需要安装jdk和scala. 大数据第三方组件我们统统都安装在/opt目 ...

  9. iptables 设置指定IP客户端访问服务器redis端口

    一.需求描述 服务器172.28.18.75开放了6379redis端口,由于没有设置登录密码,所以需要防火墙设置只能指定的IP地址172.28.5.125客户端访问redis端口 二.查看172.2 ...

  10. 牛客练习赛46 A 华华教奕奕写几何 (简单数学)

    链接:https://ac.nowcoder.com/acm/contest/894/A 来源:牛客网 华华教奕奕写几何 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...