题目描述
解题思路
运用单调递增栈即可求解。
代码
#include <iostream>
#include <stack>
using namespace std;
#define MAX 100000
long long arr[MAX + 5], l[MAX + 5], r[MAX + 5];
int main()
{
int n;
stack<int> s;
long long ans = 0;
cin >> n;
arr[0] = arr[n + 1] = -1;
for (int i = 1; i <= n; i++)
cin >> arr[i];
s.push(0);
for (int i = 1; i <= n; i++)
{
while (arr[s.top()] >= arr[i])
s.pop();
l[i] = i - s.top();
s.push(i);
}
while (!s.empty())
s.pop();
s.push(n + 1);
for (int i = n; i >= 1; i--)
{
while (arr[s.top()] >= arr[i])
s.pop();
r[i] = s.top() - i;
s.push(i);
}
for (int i = 1; i <= n; i++)
ans = max(ans, arr[i] * (r[i] + l[i] - 1));
cout << ans << endl;
return 0;
}