[POJ 2559] 最大矩形面积

Posted wyctstf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 2559] 最大矩形面积相关的知识,希望对你有一定的参考价值。

单调栈模板题

提供一种奇技淫巧的解法,均摊O(n)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define int long long
const int N = 100010;
int ans, n;
int a[N];
int l[N], r[N];
inline int read()

    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
        f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch))
        x = x * 10 + (ch - '0'), ch = getchar();
    return x * f;

main()

    // freopen("in.txt", "r", stdin);
    while (1)
    
        n = read();
        if (n == 0)
            break;
        ans = 0;
        for (int i = 1; i <= n; i++)
            a[i] = read(), l[i] = r[i] = i;
        for (int i = 1; i <= n; i++) 
            while (l[i] > 1 && a[l[i] - 1] >= a[i])
                l[i] = l[l[i] - 1];
        for (int i = n; i >= 1; i--)
            while (r[i] < n && a[r[i] + 1] >= a[i])
                r[i] = r[r[i] + 1];
        for (int i = 1; i <= n; i++)
            ans = std::max(ans, (r[i] - l[i] + 1) * a[i]);
        std::cout << ans << '\n';
    
    return 0;

以上是关于[POJ 2559] 最大矩形面积的主要内容,如果未能解决你的问题,请参考以下文章

POJ2559 Largest Rectangle in a Histogram 单调队列

POJ2559 单调栈

poj2559单调栈

《算法竞赛进阶指南》0x11栈 单调栈求矩形面积 POJ2559

poj 2559 Largest Rectangle in a Histogram 栈

POJ2559 Largest Rectangle in a Histogram(单调栈)