单调栈板子题,代码很简单。
注意将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-10-14 01:56:22