栈的应用,计算每个矩形往左往右最多能扩展到哪里。
然后再枚举一遍 O(n) 的
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 1000000
int n;
ll h[MAX];
int r[MAX];
int l[MAX];
int main()
{
READ;
while(scanf("%d",&n)&&n)
{
MS(r,0),MS(l,0);
stack<PII> S;
for(int i=1;i<=n;i++)
scanf("%lld",h+i);
h[0]=h[n+1]=-1;
for(int i=1;i<=n+1;i++)
{
while(!S.empty()&&S.top().fst>h[i])
r[S.top().sec]=i-S.top().sec,S.pop();
S.push(PII(h[i],i));
}
while(!S.empty())
S.pop();
for(int i=n;i>=0;i--)
{
while(!S.empty()&&S.top().fst>h[i])
l[S.top().sec]=S.top().sec-i,S.pop();
S.push(PII(h[i],i));
}
ll ans=-1;
for(int i=1;i<=n;i++)
{
ans=max(ans,h[i]*(r[i]+l[i]-1));
}
printf("%lld\n",ans);
}
return 0;
}