题意
一个围挡由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. CRMEB 商城系统常见错误修复办法

    清空了用户表,没有清空拼团记录表导致,解决办法 --v2.5.2 清空拼团表-- TRUNCATE table eb_store_pink 修改了组合数据规则导致的,更新数据即可 --v2.5.2 e ...

  2. LOJ #6485 LJJ 学二项式定理

    QwQ LOJ #6485 题意 求题面中那个算式 题解 墙上暴利 设$ f(x)=(sx+1)^n$ 假设求出了生成函数$ f$的各项系数显然可以算出答案 因为模$ 4$的缘故只要对于每个余数算出次 ...

  3. Mac终端命令自动补全

    在这里我们首先说一下mac终端执行命令的时候,不会像在windows系统中安装的linux一样支持自动补全,需要自己去调试 步骤如下: (1)打开终端输入nano .inputrc(这里一定要注意na ...

  4. Navicat连接阿里云服务器Linux下的Mysql

    用Navicat连接阿里云ECS服务器上的MySQL数据库   今天用navtive连接阿里云服务器(Linux)的数据库时,老是连接不上,并且报10060错误,要通过以下两个步骤解决: 1.先进入l ...

  5. word20161231

    1. measurement 英[ˈmeʒəmənt]美[ˈmeʒərmənt]n. 量度; 份量,尺寸; 测量法; (量得的) 尺寸;[例句]We took lots of measurements ...

  6. Java 多线程 - 锁优化

    http://www.cnblogs.com/pureEve/p/6421273.html https://www.cnblogs.com/mingyao123/p/7424911.html

  7. Date——js 获取当前日期到之后一个月30天的日期区间

    var dateList = []; let startDate = new Date(); let endDate = new Date(); endDate.setDate(startDate.g ...

  8. chrome扩展程序----域名助手

    音乐分享: Future Islands - <Aladdin> 中年大叔的抖腿新专辑<The Far Field> ————————————————————————————— ...

  9. QT windeployqt

    qt发布release版本时需要打包一些dll,需要哪些呢?请看截图: 在qt的安装包下找到这些文件,放在release文件夹下即可,当然有些时候也会需要一些其他的,比如含有串口的程序还需要加入Qt5 ...

  10. 阿里云服务器配置https(port443)后客户端 svn check out 失效解决办法

    1. 客户端环境 1. 操作系统:Windows 7 2. svn客户端:TortoiseSVN 2. 服务端环境 1. 云服务平台:阿里云 2. 操作系统:Windows Server 2008 R ...