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 ...
随机推荐
- HSSFClientAnchor 参数说明
pache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能. HSSFClient ...
- Confluence 6 配置 Windows 服务
当你使用 Start Confluence Automatically on Windows as a Service 的方式启动的时候,你有下面 2 种方式来配置你的系统属性:通过 command ...
- Pod 找不到头文件 解决方法
在 BuildSetting 中 搜索 User Header Search Paths 然后在下面 User Header Search Paths 中添加 ${SRCROOT} 再将后面参数改为 ...
- pytorch:修改预训练模型
torchvision中提供了很多训练好的模型,这些模型是在1000类,224*224的imagenet中训练得到的,很多时候不适合我们自己的数据,可以根据需要进行修改. 1.类别不同 # codin ...
- CentOS 7 部署 Spring Boot
Spring Boot 内嵌了tomcat .我们可以将Boot打成 jar 包丢到服务器上运行才行. Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-sta ...
- 网络扫描信息收集基于(Windows)
1.首先说明一下一款网络扫描工具,在之前的博客中我曾简要的写过关于Advance IP Scanner使用方法,最近要写网络扫描的工具,所以对这款工具做一个详细的功能细节上的介绍. 如下图 在输入框 ...
- java爬虫笔记
一.URl解释 1.URl统一资源定位符, Uniform Resource Location 也就是说是Internet上信息资源的字符串,所谓的网页抓取就是把URl地址中指定的网络资源从网络中读取 ...
- C语言实现split以某个字符分割一个字符串
方式一: 使用strtok # include <string.h> # include <stdio.h> void split(char *src,const char * ...
- 配置web pack loader 报错:Module build failed: Error: The node API for `babel` has been moved to `babel-core`.
报错如下 Module build failed: Error: The node API for `babel` has been moved to `babel-core`. 在我配置loader ...
- Just oj 2018 C语言程序设计竞赛(高级组)F:Star(结构体排序+最小生成树)
F: Star Time Limit: 1 s Memory Limit: 128 MB Submit My Status Problem Description 31世纪,人类世界的科技已 ...