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的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. 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 ...

  3. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  6. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

  8. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  9. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. Java语言学习day13--7月14日

    今日内容介绍1.循环练习2.数组方法练习 ###01奇数求和练习 * A: 奇数求和练习 * a: 题目分析 * 为了记录累加和的值,我们需要定义一个存储累加和的变量 * 我们要获取到1-100范围内 ...

  2. 使用 GO-CQHttp或mirai框架 搭建QQ的机器人

    我的博客 Go-CQHttp搭建QQ机器人 官方文档在这-->ATRU官方文档 Go-CQHttp + Atri 使用Linux系统部署 需求 服务器一台/带有Linux的机器 Python环境 ...

  3. 红旗 Linux 桌面操作系统11来了:支持国产自主CPU,全新UI风格设计,兼容面广...

    链接:https://reurl.cc/g8ke9X 红旗Linux桌面操作系统11将于1月10日开放预览版的下载,新版本具有良好的硬件兼容,支持多款国产自主CPU品牌,同时还具有丰富的外设支持及海量 ...

  4. 干货 | 手把手教你搭建一套OpenStack云平台

    1 前言 今天我们为一位朋友搭建一套OpenStack云平台. 我们使用Kolla部署stein版本的OpenStack云平台. kolla是用于自动化部署OpenStack的一个项目,它基于dock ...

  5. Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64

    编译项目报错: Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch ...

  6. 为什么列式存储会被广泛用在 OLAP 中?

    大家好,我是大D. 不知是否有小伙伴们疑问,为什么列式存储会广泛地应用在 OLAP 领域,和行式存储相比,它的优势在哪里?今天我们一起来对比下这两种存储方式的差别. 其实,列式存储并不是一项新技术,最 ...

  7. Redis 的数据过期了就会马上删除么?

    码哥,当 key 达到过期时间,Redis 就会马上删除么? 先说结论,并不会立马删除,Redis 有两种删除过期数据的策略: 定期选取部分数据删除: 惰性删除: 该命令在 Redis 2.4 版本, ...

  8. 102_Power Pivot DAX 排名后加上总排名数

    焦棚子的文章目录 请点击下载附件 1.背景 每次写rank的时候,有了排名就可以了,排名1,2,3,4,5这样不是很清晰吗?但是中国式报表的老板们说你能不能在排名后面加一个总排名数呢,就像1/5,2/ ...

  9. vue3常见问题及解决方案(四)父组件切换行,然后子组件切换tab,子组件内的数据不刷新

    问题描述 父组件切换行,然后子组件切换tab,子组件内的数据不刷新. 例如父组件为订单,子组件为订单相关商品和相关客户,商品和客户使用tab选项卡组织. 当tab显示商品页时,切换订单,商品页内容跟着 ...

  10. 深入探究MinimalApi是如何在Swagger中展示的

    前言 之前看到技术群里有同学讨论说对于MinimalApi能接入到Swagger中感到很神奇,加上Swagger的数据本身是支持OpenApi2.0和OpenApi3.0使得swagger.json成 ...