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 ...
随机推荐
- mysql的.sql文件头部 /*!32312 IF NOT EXISTS*/;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RE ...
- 优秀前端工程师必备: 我要一个新窗口: js开新窗的2种姿势
1.<a href="https://www.cnblogs.com/" title="博客园">当前页面打开博客园</a> js代码等 ...
- 主键冲突异常 DuplicateKeyException
org.springframework.dao.DuplicateKeyException Exception thrown when an attempt to insert or update d ...
- Codeforces768C Jon Snow and his Favourite Number 2017-02-21 22:24 130人阅读 评论(0) 收藏
C. Jon Snow and his Favourite Number time limit per test 4 seconds memory limit per test 256 megabyt ...
- [label][翻译][JavaScript-Translation]七个步骤让你写出更好的JavaScript代码
7 steps to better JavaScript 原文链接: http://www.creativebloq.com/netmag/7-steps-better-javascript-5141 ...
- EBS SOA 修改Web Service参数
l 需求描述 当把PL/SQL声明Load到ISG,生成WSDL并部署完毕后,需要修改PL/SQL的包头声明部分.例如修改某个过程的参数类型,再重新Load,重新生成WSDL,重新部署.我们会发现PL ...
- access建立sql查询语句运行查询语句
1.打开一个Access数据库文件 2.点击“创建”标签中的“查询设计”,会弹出一个“显示表”的对话框,点击“关闭”将其关闭 3.这时会有一个名为“查询*”的窗口,还不能输入SQL语句 4.点击左上角 ...
- Sqler-Monitor
针对Sqler Monitor 功能做了整理 ##SqlServices ## Cluster. Alwayson Single ##Replicaion ##: 1:undelivedcmds mo ...
- 【分分钟内搭建一个带用户系统的博客程序(一)用户系统】asp.net core的Identity真香,EF真香!
不用不知道,一用香到爆. 老哥是个屌丝前端,但也想写点web应用耍一耍.之前弄过了NodeJs,也弄过JAVA,最近由于写游戏的原因用C#,索性上手一波asp.net core. 这篇博客记录的是,如 ...
- C#学习(2):委托
1.疑问: 1.委托是什么? 2.为什么需要委托? 3.委托能用来做什么? 4.如何自定义委托? 5..NET默认的委托类型有哪几种? 6.怎样使用委托? 7.多播委托是什么? 8什么是泛型委托? 9 ...