Largest Rectangle in a Histogram POJ - 2559 (单调栈)
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
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
Hint
(黄色区域为回溯时,矩形高度仍大于当前矩形,更新的答案)
最后,对整个递增的矩形序列进行一次回溯,答案的更新,为了方便将其最后加入一个高度为0的矩形,当然不加另外判断也ok
(用不用栈无所谓,重要的是单调性)
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std; typedef long long ll;
const int maxn = 1e5+;
stack<ll>s;
ll ans;
int w[maxn];
int h[maxn];
int n;
int main()
{
while(~scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)scanf("%d",&h[i]);
while(!s.empty())s.pop();
int pos = ;
h[n+] = ;
ans = ;
for(int i=;i<=n+;i++)
{
if(s.empty() || h[i] >= s.top())
{
s.push(h[i]);
w[++pos] = ;
}
else
{
int width = ;
while(!s.empty() && s.top() > h[i])
{
width += w[pos];
ans = max(ans,s.top()*width);
s.pop();
pos--;
}
s.push(h[i]);
w[++pos] = width+;
}
}
printf("%lld\n",ans);
}
}
Largest Rectangle in a Histogram POJ - 2559 (单调栈)的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
- Largest Rectangle in a Histogram POJ - 2559
很显然是单调栈 这里记录一种新的写法,这种写法基于递推,但是相比之下比单调栈更好写 #include<cstdio> #include<map> #include<set ...
- HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram
http://acm.hdu.edu.cn/showproblem.php?pid=1506 || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...
- poj 2559 单调栈 ***
给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...
- 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 - 单调栈
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 ...
随机推荐
- iOS 去除百度地图下方的 logo
UIView *mView = _mapView.subviews.firstObject; for (id logoView in mView.subviews) { if ([logoView ...
- ModuleNotFoundError: No module named 'requests'
1.问题描述 DOS 执行 .py 脚本报错: ModuleNotFoundError: No module named 'requests' ModuleNotFoundError: No modu ...
- Confluence 6 注册外部小工具
你可以从外部站点中注册小工具(Gadget)(例如 Jira 应用),你注册成功的小工具将会在 宏浏览器中显示出来,使用你 Confluence 站点的用户可以使用 Gadget Macro 来调用它 ...
- Java的家庭记账本程序(B)
日期:2019.2.3 博客期:029 星期日 看看今天想先完成jsp的连接操作,所以首先意识到自己的程序中,管理员可以对成员进行冻结.解封操作,所以先回去补了一下数据库的内容!成员的内容里多了一项i ...
- GitHub搭配使用Travis CI 进行自动构建服务
Travis CI (Continuous Integration)持续集成服务 用处:自动监控软件仓库,可以在代码提交后立刻执行自动测试或构建 1.在Github自己的仓库根目录里添加.travis ...
- 《剑指offer》二叉搜索树的后序遍历序列
本题来自<剑指offer> 二叉搜索树的后序遍历序列 题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字 ...
- strchr()
strchr()主要有2个最有用的用法: 第一个:搜索字符串在另一字符串中的第一次出现.并返回剩余的部分 $str = "hello_chrdai_1993"; $not_incl ...
- 20165314 2016-2017-2 《Java程序设计》第7周学习总结
20165314 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 MySQL数据库管理系统 连接MySQL数据库 JDBC 批处理 代码托管
- SQL Server控制执行计划
为了提高性能,可以使用提示(hints)特性,包含以下三类: 查询提示:(query hints)告知优化器在整个查询过程中都应用某个提示 关联提示:(join hints)告知优化器在查询的特定部分 ...
- 解开一个疑惑,为什么LVS开放的端口,使用netstat或ss命令,不能查找到其监听的端口呢?
RT, 这个疑问,本周一直在心里,今天找到一个说法. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 另外LVS规则算是内核方法,用netstat -ntulp也显 ...