AcWing 131. 直方图中最大的矩形
原题链接
简单
作者:
Susu
,
2021-07-15 17:44:15
,
所有人可见
,
阅读 274
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
typedef long long LL;
int h[N], l[N], r[N], q[N];
int main()
{
int n;
while (cin >> n, n){
for (int i = 1; i <= n; i ++ ) cin >> 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>0 ; i -- ){
while(h[i] <= h[q[tt]]) tt--;
r[i] = q[tt];
q[++tt] = i;
}
LL res = 0;
for (int i = 1; i <= n; i ++ )
res = max(res, (LL) h[i] * (r[i] - l[i] - 1));
cout << res <<endl;
}
return 0;
}