You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 10000010;
int a[N];
int res;
/*
I
6
3 2 1 4 5 2
:
8
II
5
2 4 3 4 5
:
12
*/
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> stk;
for (int i = 0; i <= n; i++) { // 注意这里是0~n,其实数组中有效数据是0~n-1,之所以最后要多枚举一个n,是因为栈内可能存在一直找不到右手边比自己小的数据最后补一个0让它们都出栈
while (stk.size() && a[i] <= a[stk.top()]) { // 栈不为空,并且,当前柱子高度小于栈顶柱子高度,此时,栈顶柱子找到了右手边的最远距离
int h = a[stk.top()]; // 栈顶柱子高度
stk.pop(); // 栈顶柱子出栈
/*
i=65
1stk.top()35-3=2
33
5545
2500,1,2,3,4,56i
*/
int w = (stk.size() == 0 ? i : i - 1 - stk.top());
res = max(res, h * w);
}
stk.push(i);
}
cout << res << endl;
return 0;
}