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 ...
随机推荐
- 支付宝调用错误:Call to undefined function openssl_sign()
打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的“;”去掉:重启服务器
- Confluence 6 Windows 中以服务方式自动重启为服务手动安装 Confluence 分发包
在 Windows: 打开一个命令输入框,然后修改目录到 <CONFLUENCE-INSTALL>/bin 目录中.你需要以管理员权限运行这个命令行输入框(Run as administr ...
- 前端html
前端html html 是一种描述网页的语言,是超文本标记语言 :hyper Text Markup Lauguage 是一种标记语言[标记语言是一套标记标签 markup tag]使用标记标签来 ...
- package.json包描述文件说明
//commonjs包规范-说明 { "name": "leyi",//包名,不允许空格 "description": "hell ...
- ubuntu MySQL的安装
https://i.cnblogs.com/EditPosts.aspx?opt=1 https://juejin.im/entry/5adb5deff265da0b9d77cb3b MySQL Co ...
- laravel 注入那点事
public function delete(Group $groupId, Post $postId) { $postId->delete(); return response()->j ...
- models批量生成数据
models批量生成数据 1.将数据生成为 列表序列,通过 bulk_create 将数据一次插入数据库中 def host(request): # 插入数据速度快消耗资源少 Hostlist=[] ...
- epoll(二)
epoll概念 epoll对文件描述符的操作方式有两种工作模式:LT模式(Level Trigger,水平触发) 和ET模式(Edge Trigger,边缘触发). LT模式:当epoll_wait检 ...
- Android之Error: 'L' is not a valid file-based resource name character解决办法
1.问题 Error:Execution failed for task ':mergeBYODReleaseResources'.> /home/chenyu/Android_dev/sang ...
- python--使用递归优雅实现列表相加和进制转换
咦,好像坚持了一段时间,感觉又有新收获啦. # coding: utf-8 class Stack: def __init__(self): self.items = [] # 是否为空 def is ...