NC50965 Largest Rectangle in a Histogram
NC50965 Largest Rectangle in a Histogram
题目
题目描述
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
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.
输入描述
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that \(1 \leq n \leq 100000\) . Then follow n integers \(h1\dots hn\), where \(0 \leq h_i \leq 1000000000\). These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
输出描述
For 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.
示例1
输入
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
输出
8
4000
说明
Huge input, scanf is recommended.
题解
思路
知识点:单调栈。
如果枚举区间,获取区间最小直方,显然是很复杂的。因为区间不同导致的最小值不同,虽然可以用单调队列动态获取某一区间的最小值,但问题在于端点的可能有 \(n^2\) 个,所以复杂度是 \(O(n^2)\) 是不可接受的。
但是换一种角度,我们枚举直方,一共就 \(n\) 个,枚举 \(n\) 次即可。那么固定一个直方,最大的可伸展长度取决于左右第一个小于它的位置,找到长度乘以直方高度就是矩形面积了。
对于一个直方,左边最邻近小于用单调递增栈从左到右维护,右边同理从右到左维护,注意找到的位置是小于的那个直方的位置,而不是可伸展最大的位置,因此左边的需要加一,右边的需要减一。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int h[100007];
int l[100007], r[100007];
///最大矩形高度肯定是某个矩形高度
///对于一个矩形,水平扩展距离取决于第一个比他小的,两边都是
///于是对每个矩形,用单调递增栈获得他左侧/右侧第一个比它小的矩形位置,就能知道左侧/右侧扩展距离
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
while (cin >> n, n) {
for (int i = 0;i < n;i++) cin >> h[i];
stack<int> s1;
for (int i = 0;i < n;i++) {
while (!s1.empty() && h[s1.top()] >= h[i]) s1.pop();
l[i] = s1.empty() ? 0 : s1.top() + 1;///左侧大于等于的第一个位置
s1.push(i);
}
stack<int> s2;
for (int i = n - 1;i >= 0;i--) {
while (!s2.empty() && h[s2.top()] >= h[i]) s2.pop();///一定是大于等于,于是栈就是严格递减栈,元素是最靠右的
r[i] = s2.empty() ? n - 1 : s2.top() - 1;///右侧大于等于的最后一个位置
s2.push(i);
}
long long ans = 0;
for (int i = 0;i < n;i++)
ans = max(ans, (r[i] - l[i] + 1LL) * h[i]);
cout << ans << '\n';
}
return 0;
}
NC50965 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 ...
- DP专题训练之HDU 1506 Largest Rectangle in a Histogram
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- Largest Rectangle in a Histogram(HDU1506)
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
- Largest Rectangle in a Histogram
2107: Largest Rectangle in a Histogram Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 777 Solved: 22 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- 搜索与图论②--宽度优先搜索(BFS)
宽度优先搜索 例题一(献给阿尔吉侬的花束) 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫. 今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔 ...
- vue2.x版本中Object.defineProperty对象属性监听和关联
前言 在vue2.x版本官方文档中 深入响应式原理 https://cn.vuejs.org/v2/guide/reactivity.html一文的解释当中,Object.defineProperty ...
- XCTF练习题---MISC---神奇的Modbus
XCTF练习题---MISC---神奇的Modbus flag:sctf{Easy_Modbus} 解题步骤: 1.观察题目,下载附件 2.打开下载文件,发现可以用WireShark打开,打开看看是啥 ...
- HTTP协议4.14
测试开发学习笔记 一. Saas software as a service 软件即服务 Platform as a service 平台即服务 单体架构---垂直架构---面向服务架构---微服务架 ...
- GO语言学习——切片三 append()方法为切片添加元素、切片的扩容策略、使用copy()函数复制切片、从切片中删除元素
append()方法为切片添加元素 注意:通过var声明的零值切片可以在append()函数直接使用,无需初始化. var s []int s = append(s, 1, 2, 3) append( ...
- Linux-交互式转化批处理工具expect
expect 是由Don Libes基于 Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式 操作的场景,借助 expect 处理交互的命令,可以将交互过程如: ...
- 详解:什么是VXLAN?
点击上方"开源Linux",选择"设为星标"回复"学习"获取独家整理的学习资料! 本文介绍了什么是VXLAN,以及VXLAN的基本概念和工作 ...
- CentOS 7 执行 yum 命令失败问题的排查方法
一个执着于技术的公众号 简介 本文主要为大家讲解 CentOS 7系统中执行yum命令失败等常见问题的排查方法. 1.执行yum命令报404错误 1)检查yum仓库是否配置正确,可以到阿里云下载rep ...
- ShardingSphere 集成 CosId 实战
背景 在软件系统演进过程中,随着业务规模的增长 (TPS/存储容量),我们需要通过集群化部署来分摊计算.存储压力. 应用服务的无状态设计使其具备了伸缩性.在使用 Kubernetes 部署时我们只需要 ...
- ELF文件结构
ELF文件结构 ELF文件的全称是Executable and Linkable Format,直译为"可执行可链接格式",包括目标文件(.o).可执行文件(可以直接运行).静态链 ...