用单调栈维护高度,使得栈内高度递增,每次一个高度h出栈时说明以h为高的这个矩阵不可能再延伸了,我们就计算出这个矩阵的大小,更新ans。还要注意维护宽度,比如进来的是高度h,为此删掉了3个点,则这3个点一定都比h高,第四个点就没有h高了,也就不能再向左延伸,则h向左延伸的最大长度也就是这3个点的宽度和了,而向右延伸的最大宽度则是在这个点出栈时已经删掉的点的宽度和(这个点出栈,说明遇到了比他小的高度,不能再向右延伸了)。
#include <cstdio>
#include <cstring>
#define ll long long
#define N 100005
int n;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline ll max(ll x,ll y){return x>y?x:y;}
struct node{
int h,w;
};
int main(){
// freopen("a.in","r",stdin);
while(1){
n=read();if(!n) break;
node stack[N];int top=0;ll ans=0;
while(n--){
int h=read(),tmp=0;
while(top&&h<=stack[top].h){
stack[top].w+=tmp;
ans=max(ans,(ll)stack[top].h*stack[top].w);
tmp=stack[top--].w;
}
stack[++top].h=h;stack[top].w=1+tmp;
}int tmp=0;
while(top){
stack[top].w+=tmp;
ans=max(ans,(ll)stack[top].h*stack[top].w);
tmp=stack[top--].w;
}
printf("%lld\n",ans);
}
return 0;
}