题意
一个围挡由n个宽度为1的长方形挡板下端对齐后得到,每个长方形挡板的高度为hi。我们把其抽象成一个图形,问这个图形中包含的面积最大的长方形是多大?

输入
多行数据,每行第一个为n,后面n个数,代表hi
以0为结束

输出
每行一个数

样例输入
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
样例输出
8
4000

分析
我们定一个中心为i,矩形高度为Hi,设他能在一个区间[l,r]中存在,必满足j∈[l,r]使Hj≥Hi。如果Hl-1≥Hi,显然可以继续向右扩张,那么Hl-1一定小于Hi,所以l-1是[1,i]中最后一个小于Hi的。那我们从左向右扫维护所有比Hi小的标号,形成一个单调递增的栈,栈顶即是他的左边界。同理维护右边界。

代码

 #include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
#define maxn 100005
using namespace std;
int n;
ll num[maxn],L[maxn],R[maxn];
struct D{
int h,id;
};
stack<D> stk;
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void work()
{
int id;
while(!stk.empty()) stk.pop();
rep(i,,n)
{
while(!stk.empty()&&stk.top().h>=num[i]) stk.pop();
if(!stk.empty()) id=stk.top().id;
else id=;
L[i]=id+;
stk.push((D){num[i],i});
}
while(!stk.empty()) stk.pop();
per(i,n,)
{
while(!stk.empty()&&stk.top().h>=num[i]) stk.pop();
if(!stk.empty()) id=stk.top().id;
else id=n+;
R[i]=id-;
stk.push((D){num[i],i});
}
ll ans=;
rep(i,,n) ans=max(ans,num[i]*(R[i]-L[i]+));
printf("%lld\n",ans);
} int main()
{
//freopen("a","r",stdin);
while()
{
n=read();if(!n) return ;
rep(i,,n) num[i]=read();
work();
}
return ;
}

Largest Rectangle in a Histogram [POJ2559] [单调栈]的更多相关文章

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

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

  2. POJ2559 Largest Rectangle in a Histogram (单调栈

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

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

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

  4. 题解报告: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 ...

  5. HDU-1506 Largest Rectangle in a Histogram【单调栈】

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

  6. Largest Rectangle in a Histogram【单调栈模板】

    Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...

  7. ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」

    类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...

  8. hdu_1506:Largest Rectangle in a Histogram 【单调栈】

    题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...

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

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

随机推荐

  1. slim.arg_scope中python技巧

    slim.arg_scope函数说明如下: Stores the default arguments for the given set of list_ops. For usage, please ...

  2. 在linux安装mysql重启提示You must SET PASSWORD before executing this statement的解决方法

    利用安全模式成功登陆,然后修改密码,等于给MySql设置了密码.登陆进去后,想查询所有存在的数据库测试下.得到的结果确实: ERROR 1820 (HY000): You must SET PASSW ...

  3. Angular_上拉刷新

    1.先不做上拉触发,用button模拟一下,触发函数 export class StudyComponent implements OnInit { /*列表数据流 */ list$: Observa ...

  4. 常用数据库:MongoDB

    下载地址:https://www.mongodb.com/download-center/community 安装及配置指南:https://docs.mongodb.com/manual/insta ...

  5. 关于操作HDFS的一个问题

    近日写程序定时任务调Hadoop MR程序,然后生成报表,发送邮件,当时起了两个任务A和B,调MR程序之前,会操作hdfs(读写都有),任务A每天一点跑,任务B每十分钟跑一次,B任务不会调用MR程序, ...

  6. python爬虫得到unicode编码处理方式

    在用python做爬虫的时候经常会与到结果中包含unicode编码,需要将结果转化为中文,处理方式如下 str.encode('utf-8').decode('unicode_escape')

  7. 关于docker使用

    docker讲解:http://dockone.io/article/6051 os: ubuntu 16.04;docker version 18.06.1-ce; (1)查看docker版本:do ...

  8. shell编程练习-打印九九乘法表(附:awk编程)

    小练习,仅供参考 shell编写 #!/bin/bash for i in {1..9}do for j in {1..9} do if [ $j -le $i ] ;then echo -ne &q ...

  9. Java中的String、StringBuilder以及StringBuffer

    https://www.cnblogs.com/dolphin0520/p/3778589.html

  10. ubuntu 配置apt-get源

    ubantu安装软件速度慢一般是因为系统默认选择的源导致,可以通过手动配置源设置解决. 1. 原文件备份 sudo mv /etc/apt/sources.list /etc/apt/sources. ...