当前位置:网站首页>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* ̄▽ ̄*

 Insert picture description here
 Insert picture description here
 Insert picture description here
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.

 Insert picture description here

Ideas :

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

 Insert picture description here
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

 Insert picture description here

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;
}

原网站

版权声明
本文为[Jacob* ̄▽ ̄*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121032040462.html