AcWing 131. 直方图中最大的矩形
原题链接
简单
作者:
wjie
,
2020-07-25 21:46:52
,
所有人可见
,
阅读 562
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5;
LL h[N];
int lef[N], rig[N];
int main()
{
int n;
while (scanf("%d", &n), n)
{
for (int i = 1; i <= n; i++) scanf("%lld", &h[i]);
h[0] = h[n+1] = -1;
vector<int> vec;
vec.push_back(0);
for (int i = 1; i <= n; i++)
{
while (h[vec.back()] >= h[i]) vec.pop_back();
lef[i] = vec.back();
vec.push_back(i);
}
// for (int i = 1; i <= n; i++) cout << lef[i] << " ";
// cout << endl;
vec.clear();
vec.push_back(n+1);
for (int i = n; i >= 1; i--)
{
while (h[vec.back()] >= h[i]) vec.pop_back();
rig[i] = vec.back();
vec.push_back(i);
}
// for (int i = 1; i <= n; i++) cout << rig[i] << " ";
// cout << endl;
LL res = 0;
for (int i = 1; i <= n; ++i)
{
res = max(res, h[i] * (rig[i]-lef[i]-1));
}
printf("%lld\n", res);
}
return 0;
}