AcWing 131. 直方图中最大的矩形
原题链接
简单
作者:
grairain
,
2021-05-18 20:37:52
,
所有人可见
,
阅读 215
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
int n;
int h[N], q[N], l[N], r[N];
int main()
{
while(cin >> n, n) {
for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
h[0] = h[n + 1] = -1;
q[0] = 0;
int tt = 0;
for(int i = 1; i <= n; i ++ ) {
while(h[i] <= h[q[tt]]) tt -- ;
l[i] = q[tt];
q[++ tt] = i;
}
tt = 0;
q[0] = n + 1;
for(int i = n; i >= 1; i -- ) {
while(h[i] <= h[q[tt]]) tt -- ;
r[i] = q[tt];
q[++ tt] = i;
}
long long res = 0;
for(int i = 1; i <= n; i ++ ) {
res = max(res, (long long)h[i] * (r[i] - l[i] - 1));
}
printf("%lld\n", res);
}
return 0;
}