51Nod - 1102 单调栈
2017-02-03 22:22
197 查看
题意:
有一个正整数的数组,化为直方图,求此直方图包含的最大矩形面积。例如 2,1,5,6,2,3,对应的直方图如下:面积最大的矩形为5,6组成的宽度为2的矩形,面积为10。
Input
第1行:1个数N,表示数组的长度(0 <= N <= 50000) 第2 - N + 1行:数组元素A[i]。(1 <= A[i] <= 10^9)
Output
输出最大的矩形面积
Input示例
6 2 1 5 6 2 3
Output示例
10
思路:
单调栈的模板题,枚举最低点,然后找到左右的边界。代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5e4 + 10; ll a[MAXN]; int l[MAXN], r[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); stack <int> sta; for (int i = 1; i <= n; i++) { while (!sta.empty() && a[sta.top()] >= a[i]) sta.pop(); l[i] = sta.empty() ? 0 : sta.top(); sta.push(i); } while (!sta.empty()) sta.pop(); for (int i = n; i >= 1; i--) { while (!sta.empty() && a[sta.top()] >= a[i]) sta.pop(); r[i] = sta.empty() ? n + 1 : sta.top(); sta.push(i); } ll ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, (r[i] - l[i] - 1) * a[i]); printf("%I64d\n", ans); return 0; }
相关文章推荐
- 51nod 1102 单调栈
- 51nod 1102 【单调栈】
- 51nod 1102 单调栈
- 51nod 1102单调栈
- [51nod 1158 全是1的最大子矩阵] 单调栈
- 岛屿的数量 51Nod - 1276(思维+贪心+单调)
- 51Nod - 1158 全是1的最大子矩阵 单调栈
- 51NOD 1952 栈 【单调队列】
- 51nod-【1102 面积最大的矩形】
- 51Nod - 1215 数组的宽度 思维+单调栈
- 51Nod-1102-面积最大的矩形
- 51nod-1102 面积最大的矩形
- 51nod 1288 汽油补给[贪心][st表][单调栈]
- 51nod 1952 栈【单调队列】
- 51Nod 1158 最大子矩阵变形 前缀和+ DP || 单调栈
- 51Nod 1102 面积最大的矩形 (左右最大上升区间)
- 51nod 1102 面积最大的矩形
- 【51Nod】1102 - 面积最大的矩形面积(单调栈)
- 51Nod 1292 字符串中的最大值 V2 后缀数组 + 单调栈
- 51NOD 1102 面积最大的矩形