PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接
一排紧密相连的矩形,求能构成的最大矩形面积。
为了防止栈为空,所以提前加入元素(-1,0)
#include<cstdio>
#include<stack>
#define maxn 100005
using namespace std;
long long h[maxn];
struct Element
{
long long height;
int startpos;
Element(){}
Element(long long _h,int _p){
height=_h,startpos=_p;
}
/*Element(long long _h, int _p):height(_h), startpos(_p)
{
//和上面的一样的效果
}*/
};
int n,now,curpos;
long long ans,curArea,maxArea,curheight;
long long get_Max()
{
stack<Element> S;
S.push(Element(-,));
maxArea=;
for(int i=;i<=n;i++){
curpos=i+;
if(i==n)
curheight=;
else
curheight=h[curpos-];
Element now(curheight,curpos);
while(S.top().height>curheight){
now=S.top();
S.pop();
curArea=(curpos-now.startpos)*now.height;
if(curArea>maxArea)
maxArea=curArea;
}
S.push(Element(curheight,now.startpos));//注意入栈的now.startpos,不是curpos
}
return maxArea;
}
int main()
{
while(scanf("%d",&n),n){
for(int i=;i<n;i++)
scanf("%lld",&h[i]);
ans=get_Max();//get_Max()有返回值,傻逼,记得写一个ans
printf("%lld\n",ans);
}
}
PKU 2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29498 ...
- HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- Largest Rectangle in a Histogram /// 单调栈 oj23906
题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...
随机推荐
- Effective C++ 条款26
尽可能延后变量定义式的出现时间 我们知道定义一个对象的时候有一个不争的事实,那就是分配内存.假设是我们自己定义的对象.程序运行过程中会调用类的构造函数和析构函数. 我们打个例如,假设天下雨了,你带把雨 ...
- myeclipse10中对象无法点出下面的方法和属性
刚安装完最新的myeclipse4spring-10.6,发现通过对象无法点出下面的方法和属性,没有自动联想或自动补全,使用“Alt + /”也没得效果,解决方法如下: Window -> Pr ...
- 如何用ChemDraw绘制化学课件
近年来随着ChemDraw等多媒体技术的迅速发展,多媒体技术越来越多的应用在教学中.学会应用ChemDraw绘制化学分子结构.化学反应式和实验装置的方法,将在有机化学的教学中提供一定的帮助,进一步提高 ...
- 判断ActiveX控件是Desgin Mode还是Runtime Mode
对于MFC COleControl::AmbientUserMode Determines if the Container is in design mode or user mode. BOOL ...
- Scilab 的画图函数(3)
我们在做数据画图或函数图像时常常须要使用对数坐标系.尤其是数据的范围跨越非常多个数量级时.通常的线性坐标系下无法表现出数据特征. Scilab 中Plot函数无法画出对数坐标.须要使用 plot2d ...
- malloc free, new delete 的异同点
相同点: 都可以动态的申请并释放内存 不同点: 1. 用法不同 <1> malloc 函数为 void* malloc(size_t size), 用于申请一块长度为 size 字节的内存 ...
- phpcms V9内容页调用标签
1.页面标题:{$title} 2.发表时间:{$inputtime} 3.内容来源:{$copyfrom} 4.文章内容:{$content} 5.缩略图地址:{$thumb} 6.组图列表: {l ...
- ios 如何改变UISegmentedControl文本的字体大小?
UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes = [NSDictionary dic ...
- ZOJ 3932 Deque and Balls
There are n balls, where the i-th ball is labeled as pi. You are going to put n balls into a deque. ...
- 解决CMD编译中文乱码
chcp 是MD DOS中的命令,用了显示或设置活动的代码编号的,用法查看当前编码格式(系统默认为:936 #936表示的是gb2312) chcp 修改当前编码格式(修改为utf-8) chcp ...