Largest Rectangle in a Histogram [POJ2559] [单调栈]
题意
一个围挡由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] [单调栈]的更多相关文章
- poj2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- POJ2559 Largest Rectangle in a Histogram (单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26012 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- 题解报告: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 ...
- 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 ...
- Largest Rectangle in a Histogram【单调栈模板】
Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...
- ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」
类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...
- hdu_1506:Largest Rectangle in a Histogram 【单调栈】
题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
随机推荐
- IE8 环境的 JQuery 中的 $.ajax 拒绝访问---解决方案
需求是兼容到 IE8. $.ajax 总是请求失败,直接跳转到 error 函数中,报错:拒绝访问. 网上的一些解决方法都试过了还是没有用,比如再请求之前设置 jQuery.support.cors ...
- Now you can provide attr "wx:key" for a "wx:for" to improve performance. 微信小程序警告
Now you can provide attr "wx:key" for a "wx:for" to improve performance为警告,不处理不影 ...
- temp 和 tmp 文件
TMP和TEMP文件是各种软件或系统产生的临时文件,也就是常说的垃圾文件.Windows产生的临时文件,本质上和虚拟内存没什么两样,只不过临时文件比虚拟内存更具有针对性,单独为某个程序服务而已.而它的 ...
- twfont
module game { /** *Created by 渔歌烟火 on 2018/3/28. * 字体缓动动画 */ export class LabelEffect { private stat ...
- Grunt 一个专为JavaScript提供的构建工具
新手最好找个视频来看看, Grunt的配置及使用(压缩合并js/css) - 每天都记录一点点! - CSDN博客https://blog.csdn.net/playboyanta123/articl ...
- 清北学堂学习总结day2
今天是钟皓曦大佬讲课,先来膜一波 %%%%% •数论 数论是这次培训的一个重点,那么什么是数论呢? 数论是研究整数性质的东西,所以理论上day2不会涉及小数QwQ (切入正题) •整除性: 设a, ...
- 服务器 隐藏php版本,nginx版本号等
隐藏php版本号: 打开php.ini配置文件 找到 expose_php 关键修改为 off 即可 重启后 web头部就不会有了 隐藏 nginx 服务器版本号: 打开nginx配置文件,在htt ...
- ebe
ebe1 Email:kefu007@vip.qq.com 89600 VSA 软件解调和矢量信号分析的综合工具 Aspen Plus大型通用流程模拟系统 AutoAdvance Baccarat-P ...
- 使用 eclipse 的常用操作
1.创建项目 https://blog.csdn.net/tsundere_ning/article/details/79587060 2. 常用代码块创建编辑 使得eclipse 相应, 点击右上角 ...
- win10下caffe安装与mnist测试实验注意点
caffe安装 安装内容:win10教育版+anaconda2+python(无gpu版本) 安装教程:主要依照三年一梦教程:https://www.cnblogs.com/king-lps/p/65 ...