当前位置:网站首页>AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)
2022-06-12 10:40:00 【Jacob* ̄▽ ̄*】



Pre knowledge : Monotonic stack
The question :
Given many column graphs , Each column The width is 1.
All the columns are close together .
We know Height of all columns h.
Find the area of the largest rectangle contained within the union of these columns ( In the following illustration , The answer is the area of the shadow ), Number of columns ≤ 10 ^ 5.

Ideas :
Consider violence first , The height of each rectangle shall prevail , Extend to both sides , Until you meet someone shorter than it

As shown in the figure , Each rectangle i Extend to both sides , obviously When Extend left To The first rectangle position that is strictly smaller than the current height le[i] Stop when , The right same principle extends to ri[i]( This is the classic use of monotone stack )
The expanded rectangular area is s = (ri[i] - le[i] - 1) ∗ h[i]
The optimal solution will be generated in these extended rectangles .( Violence O(n ^ 2))
Monotone stack optimization
When calculating the left bound of each rectangle that can be extended , It can be found that some rectangles can be ignored
As shown in the figure , because 2 The existence of rectangle No , In the calculation 2 The left boundary of the rectangle on the right , Not to be considered 1 No. rectangle .
The height is higher than 2 The rectangle of number will be 2 Get stuck , The height is less than or equal to 2 No. must be less than or equal to 1 Number .
Observation can be seen , When calculating the left boundary , The left and higher rectangle can be omitted , So you can use Monotone stack optimization .
stk[tt] As Stack top element subscript , When Satisfy h[stk[tt]] >= h[i] when , eject To the top of the stack

Time complexity :
O ( n ) O(n) O(n)
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n;
int h[N];
int stk[N], tt;
int le[N], ri[N];
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
while (cin >> n, n)
{
memset(le, 0, sizeof le);
memset(ri, 0, sizeof ri);
for (int i = 1; i <= n; ++i) scanf("%lld", &h[i]);
tt = 0;
for (int i = 1; i <= n; ++i)
{
while (tt && h[stk[tt]] >= h[i]) --tt;
if (tt) le[i] = stk[tt];
else le[i] = 0;
stk[++tt] = i;
}
tt = 0;
for (int i = n; i >= 1; --i)
{
while (tt && h[stk[tt]] >= h[i]) --tt;
if (tt) ri[i] = stk[tt];
else ri[i] = n + 1;
stk[++tt] = i;
}
int res = -1;
for (int i = 1; i <= n; ++i)
{
res = max(res, h[i] * (ri[i] - le[i] - 1));
}
cout << res << '\n';
}
}
return 0;
}
边栏推荐
- The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
- Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
- Pycharm view the current version of opencv
- 元宇宙系统搭建与构造
- CONDA install tensorflow test tensorflow
- Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
- Flex layout
- 数组,整型,字符变量在全局和局部的存在形式
- PHP uses RSA segment encryption and decryption method
- JS scale down the width and height of the picture
猜你喜欢

PHP download station B video

浅谈调和形状上下文特征HSC对3DSC的改进

Flex layout

Stream as a return value in WCF - who disposes of it- Stream as a return value in WCF - who disposes it?

使用cpolar远程办公(2)

Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution
![[experiment] MySQL master-slave replication and read-write separation](/img/aa/7d0799013ff749cacf44ba3b773dff.png)
[experiment] MySQL master-slave replication and read-write separation

Properties Chinese garbled code

2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?

性能指标的信仰危机
随机推荐
FPGA开发——Hello_world例程
PHP can load different new data methods for each refresh
Composer command
M-arch (fanwai 11) gd32l233 evaluation PWM driven active buzzer
golang中的定时器
PHP wechat red packet allocation logic
k52.第一章 基于kubeadm安装kubernetes v1.22 -- 集群部署
淘宝618超级喵运会怎么玩?超级喵运会整体活动攻略来了
JS open common application market
Php:redis uses geospatial
The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet
Leetcode 2169. 得到 0 的操作数
Solution to invalid small program scroll into view
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
Valentina Studio Pro for Mac(mac数据库管理软件)
AcWing 41. 包含min函数的栈(单调栈)
2022淘宝618超级喵运会玩法攻略 618超级喵运会玩法技巧
How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games
Solution to the problem that the applet developer tool cannot input simplified Chinese
Introduction to encoding formats (ASCII, Unicode and UTF-8)