解题报告 『HISTOGRA - Largest Rectangle in a Histogram(单调栈)』

原题地址

单调栈板子题,代码很简单。

注意将a[n + 1]赋值为0,防止栈中矩形未弹完。

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define rep(i, a, b) for (register int i = a; i <= b; i++)

const int maxn = 1e5 + 5;

int n;
int a[maxn], sta[maxn], wid[maxn];

LL MAX(LL a, LL b) {return a > b ? a : b;}

void origin() {memset(sta, 0, sizeof(sta));}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    while (cin >> n && n) {
        origin();
        rep(i, 1, n) cin >> a[i];
        int p = a[n + 1] = 0;
        LL ans = 0;
        rep(i, 1, n + 1) {
            if (a[i] > sta[p]) {
                sta[++p] = a[i];
                wid[p] = 1;
            }
            else {
                int width = 0;
                while (a[i] < sta[p]) {
                    width += wid[p];
                    ans = MAX(ans, (LL)width * sta[p]);
                    p--;
                }
                sta[++p] = a[i], wid[p] = width + 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Kirisame-Marisa/p/11189989.html

时间: 2024-08-05 21:27:08

解题报告 『HISTOGRA - Largest Rectangle in a Histogram(单调栈)』的相关文章

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

Description 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 rectang

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

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

hdu 1506 Largest Rectangle in a Histogram 单调栈

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[100100],q[100100],l[100100],r[100100]; int main() { int i,n,cnt; while(scanf("%d",&n),n!=0) { for(i=1;i<=n;i++) { scanf("%d",

HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的高度乘上左右边界的宽度求最大值就行了. 也可以用笛卡尔树上dp的方法搞一搞,即用每个结点权值和所在子树的大小分别表示高度和宽度.(建笛卡尔树的过程也用到了单调栈,作用是维护右链) 单调栈做法: 1 #include<bits/stdc++.h> 2 using namespace std; 3 t

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem Description 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

解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)

题目出处:https://leetcode.com/problems/largest-rectangle-in-histogram/题意描述:给定n个非负的整数,代表n个依次相邻的宽度为1的柱形的高,求这些柱形所能形成的最大的矩形面积. 解决思路:此题最直接最原始的做法就是扫描起点和终点,并随时更新最大面积,但是这样的做法的复杂度为O(n^2),显然会超时,这里就不再贴代码了. 于是我们需要考虑怎么将复杂度降下来,一种想法是在求面积之前进行预处理,将每个整数左右的第一个比当前位置矮的柱形的下标l

HISTOGRA - Largest Rectangle in a Histogram

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const int maxn=1e5+7; 7 int n,fr1,fr2,tl1,tl2,ans; 8 int h[maxn],l[maxn],r[maxn],q1[maxn],q2[maxn]; 9 void pre(){ 10 memset(