po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29498 | Accepted: 9539 |
Description

Usually, histograms are used to represent discrete distributions,
e.g., the frequencies of characters in texts. Note that the order of the
rectangles, i.e., their heights, is important. Calculate the area of
the largest rectangle in a histogram that is aligned at the common base
line, too. The figure on the right shows the largest aligned rectangle
for the depicted histogram.
Input
Output
each test case output on a single line the area of the largest
rectangle in the specified histogram. Remember that this rectangle must
be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
Hint
//单调递增栈
#include<iostream>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,s,top;
int main()
{
while(~scanf("%lld",&n))
{
if(n==)
break;
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=-;//为找比a[n-1]小的数准备,因为是递增栈,将a[n]设为最小值
s=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]>=a[p.top()])//看题目要求是否要严格单调递增,这里只要求递增
p.push(i);
else
{
while(!p.empty()&&a[i]<a[p.top()])//找到第一个小于栈顶元素的数的下标
{
top=p.top();
p.pop();//只在在出栈的过程中以a[top]为最小值更新面积s
if(s<(i-top)*a[top])
s=(i-top)*a[top];
}
p.push(top);//只将延伸到最左端的元素入栈,并且以最左端的元素的!坐标!为起点,找下一个比a[i]大的最长增区间
a[top]=a[i];//修改该位置的值为a[i] }
}
printf("%lld\n",s);
}
return ; }
po'j2559 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 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- 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 (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- 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 ...
随机推荐
- 【转载】【JAVA秒会技术之图片上传】基于Nginx及FastDFS,完成图片的上传及展示
基于Nginx及FastDFS,完成商品图片的上传及展示 一.传统图片存储及展示方式 存在问题: 1)大并发量上传访问图片时,需要对web应用做负载均衡,但是会存在图片共享问题 2)web应用服务器的 ...
- ASIHTTPRequest-Cookie的使用[转]
ASIHTTPRequest允许你使用全局存储来和所有使用CFNetwork或者NSURLRequest接口的程序共享cookie. 如果设置useCookiePersistence为YES(默认值) ...
- swift UITabelVIew - 纯代码自定义tabelViewCell
// // CustomTableViewCell.swift // tab // // Created by su on 15/12/7. // Copyright © 2015年 tian ...
- iOS9 视频播放
self.videoFileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", self ...
- java中null转换成其它类型
对null进行强转会不会抛错.测试结果是,如果把null强转给对象,是不会抛异常的,因为本身对象是可以为null的.但是如果是基本类型,比如 int i = (Integer)obj的强转,其实内部会 ...
- Ubuntu下的apache2的配置过程
参考apache2的中文文档:http://httpd.apache.org/docs/2.4/ 安装apache2: apt-get install apache2 安装apache2doc文档:a ...
- 修改VS中的附加依赖项的继承值
工程用不到的库,想去都去不掉,一直链接错误... 解决方法:打开vs的“属性管理器”窗口.通过这个窗口就可以对里面的继承值进行编辑了 另,“属性管理器”这个窗口,一般在“其他窗口”选项里(至少VS20 ...
- Android-SQLiteOpenHelper里增删改查
为什么要写一篇,Android-SQLiteOpenHelper里增删改查,的文章呢: 因为之前的方式是:MySQLiteOpenHelper(只负责 生成打开据库/生成打开表/升级表),在其他端:完 ...
- Javascript几个时髦的hack技巧《Javascript Hacks for Hipsters》
转自:http://berzniz.com/post/68001735765/javascript-hacks-for-hipsters Javascript Hacks for Hipsters J ...
- [转载]MVC、MVP以及Model2(下)
通过采用MVC模式,我们可以将可视化UI元素的呈现.UI处理逻辑和业务逻辑分别定义在View.Controller和Model中,但是对于三者之间的交互,MVC并没有进行严格的限制.最为典型的就是允许 ...