进阶题链接-> 152城市游戏
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
// q存储h的下标, l,r也是存储下标
int h[N], q[N], l[N], r[N], tt;
int main()
{
int n;
while(cin >> n, n)
{
for(int i = 1; i <= n; i ++ ) cin >> h[i];
h[0] = h[n + 1] = -1; //设置边界, 因为l,r存储的是第一个小于他的值,故l[1]的值可能是0
tt = 0;
q[tt] = 0; //往左找最多只能找到 0 这个下标
for(int i = 1; i <= n; i ++ )
{
while(h[q[tt]] >= h[i]) tt -- ;
l[i] = q[tt]; // 找到第一个 小于h[i]的下标
q[ ++ tt ] = i;
}
tt = 0;
q[tt] = n + 1; // 往右找最多只能找到n + 1这个下标
for(int i = n; i; i -- )
{
while(h[q[tt]] >= h[i]) tt -- ;
r[i] = q[tt];
q[ ++ tt ] = i;
}
LL res = 0;
for (int i = 1; i <= n; i ++ )
res = max(res, (LL)h[i] * (r[i] - l[i] - 1)); //注意爆int, LL 进行强制转换
cout << res << endl;
}
return 0;
}