标签:i++ git bre 模板题 lin getchar char clu 面积
单调栈模板题
提供一种奇技淫巧的解法,均摊O(n)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define int long long
const int N = 100010;
int ans, n;
int a[N];
int l[N], r[N];
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch))
x = x * 10 + (ch - '0'), ch = getchar();
return x * f;
}
main()
{
// freopen("in.txt", "r", stdin);
while (1)
{
n = read();
if (n == 0)
break;
ans = 0;
for (int i = 1; i <= n; i++)
a[i] = read(), l[i] = r[i] = i;
for (int i = 1; i <= n; i++)
while (l[i] > 1 && a[l[i] - 1] >= a[i])
l[i] = l[l[i] - 1];
for (int i = n; i >= 1; i--)
while (r[i] < n && a[r[i] + 1] >= a[i])
r[i] = r[r[i] + 1];
for (int i = 1; i <= n; i++)
ans = std::max(ans, (r[i] - l[i] + 1) * a[i]);
std::cout << ans << '\n';
}
return 0;
}
标签:i++ git bre 模板题 lin getchar char clu 面积
原文地址:https://www.cnblogs.com/wyctstf/p/11367492.html