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 ...
随机推荐
- windows+mysql集群搭建-三分钟搞定集群
注:本文来源: 陈晓婵 < windows+mysql集群搭建-三分钟搞定集群 > 一:mysql集群搭建教程-基础篇 计算机一级考试系统要用集群,目标是把集群搭建起来,保证一 ...
- Confluence 6 管理协同编辑 - 代理和 SSL 的考虑
对于你如何连接 Synchrony 是与你的环境有关的.我们知道绝大部分的 Confluence 站点是运行在反向代理后面的,同时还使用了 SSL.这里是帮助你在你环境中识别正确的配置的一些信息和一 ...
- 设置外部查找工具来索引 Confluence 6
任何网页的 crawler 工具都可以被用来索引你的 Confluence 站点中的内容.如果你希望注册用户才能够查看的内容也被索引的话,你需要为你的 Confluence 创建一个只被 crawl ...
- Confluence 6 查看系统信息
系统信息界面提供了有关 Confluence 的配置信息和 Confluence 部署的环境信息. 希望对你的系统信息进行查看: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Confi ...
- Confluence 6 整合到支持的附件存储选项
如果你现在正在存储附件到 WebDav 或者你的数据库中.你可以整合附件的存储到文件系统中.当你的附件从数据库中被合并到文件系统后,你存储在数据库中的附件数据就可以从数据库中删除了. 当附件合并进行的 ...
- Confluence 6 基于 Confluence 数据中心的 SAML 单点登录
安全申明标记语言(Security Assertion Markup Language (SAML))是一个基于 XML 的数据格式,允许各个软件平台通过identity provider (IdP) ...
- 广工赛-hdu6468构造十叉树
是个以前没见过的模板题.. 我用比较复杂度方式过掉了.. 构造一个十叉树(有点trie的味道)来存数字,然后字典序就是先序遍历的结果 #include<bits/stdc++.h> usi ...
- 步步为营-93-MVC+EF简单实例
1:创建MVC项目 2:添加EF数据(这里选择DataBaseFirst模式) 3:添加控制器 3.1.1 创建列表页面 3.1.2 html页面 @using MvcApplication1 @{ ...
- ZOJ 4057 XOR Clique(位运算)
XOR Clique BaoBao has a sequence a1,a2,...,an. He would like to find a subset S of {1,2,...,n} s ...
- idea svn配置报错:Can't use Subversion command line client:svn
1. 在Intellij IDEA里checkout东西时出先这个错误提示:Can't use Subversion command line client:svnSubversion command ...