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