【单调栈】hdu1506 Largest Rectangle in a Histogram
单调栈的介绍及一些基本性质
http://blog.csdn.net/liujian20150808/article/details/50752861
依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里。
每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1。
然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可。
注意最后把所有的矩形出栈,更新答案。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
struct data
{
int l,h,p;
data(const int &X,const int &Y,const int &Z)
{
l=X;
h=Y;
p=Z;
}
data(){}
};
stack<data>st;
int n,a[100010];
ll ans;
int main()
{
while(1)
{
scanf("%d",&n);
if(!n)
break;
ans=0;
st.push(data(0,-1,0));
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
a[n+1]=-1;
for(int i=1;i<=n+1;++i)
{
while((!st.empty()) && a[i]<=st.top().h)
{
ans=max(ans,(ll)(i-st.top().l)*(ll)st.top().h);
st.pop();
}
if(!st.empty())
st.push(data(st.top().p+1,a[i],i));
}
cout<<ans<<endl;
}
return 0;
}
【单调栈】hdu1506 Largest Rectangle in a Histogram的更多相关文章
- 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 ...
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...
- 【题解】hdu1506 Largest Rectangle in a Histogram
目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu1506——Largest Rectangle in a Histogram
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu1506 Largest Rectangle in a Histogram
Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...
- HDU1506 Largest Rectangle in a Histogram (动规)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)
单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来 ...
- [hdu1506 Largest Rectangle in a Histogram]笛卡尔树
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506 如图,求最大的矩形面积 思路: 笛卡尔树:笛卡尔树是一棵二叉树,树的每个节点有两个值,一个为key, ...
随机推荐
- Mac linux 安装memcached服务 用法
今天在Mac上安装memcached服务的时候 由于安装memcached之前需要安装libevent依赖包 所以使用brew install libevent 安装过程中报错 Warning: Yo ...
- Apache添加mime类型
今天同事给我提了一个bug,说IE浏览器无法下载网站上的固件版本文件(xxxx.img),点击下载后显示一堆二进制乱码,因为我们公司的固件版本文件是以.img结尾的,所以对应的content-type ...
- UILabel 详解
转自:http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041 ·UILable是iPhone界面最基本的控件,主要用来显示文本信息.· ...
- GB2312转unicode程序(转)
GB2312转unicode程序 #ifndef UNICODE_H #define UNICODE_H #include <string.h> #ifdef __DEFLINUX_ ...
- 提示:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components.错误
ArcGIS10,然后就使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapControl控件. 接着编译应用程序,编译成功. 然后单击F5运行程序, ...
- BNU OJ 50997 BQG's Programming Contest
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- iOS中"查看更多/收起"功能实现
实现效果如图: 查看更多功能在很多app种都有应用,在这里简单的实现,介绍实现流程: 一个tableViewCell中包含一个collectionView,"查看更多"按钮是tab ...
- Jedis使用示例
http://javacrazyer.iteye.com/blog/1840161 http://www.cnblogs.com/edisonfeng/p/3571870.html
- Tinyproxy
Tinyproxy Tinyproxy is a light-weight HTTP/HTTPS proxy daemon for POSIX operating systems. Designed ...
- mousewheel,DOMMouseScroll判断滚轮滚动方向
firefox使用DOMMouseScroll,其他浏览器使用mousewheel 首先绑定一个滚动事件 //firefox使用DOMMouseScroll,其他浏览器使用mousewheel$(do ...