解题思路
很久之前讲的题来填坑。。单调栈,stk[top][1/2] 1代表高度,2代表宽度。 每次加入元素时将栈顶比该元素大的出栈,并且用宽度乘高度更新答案,将宽度向后累加。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
const int MAXN = 100005;
int n,a[MAXN];
int top,stk[MAXN][3];
LL ans;
int main(){
while(~scanf("%d",&n),n){
ans=0;
for(register int i=1;i<=n;i++) {
scanf("%d",&a[i]);
int tmp=0;
while(top && stk[top][1]>=a[i]){
ans=max(ans,(LL)stk[top][1]*(stk[top][2]+tmp));
tmp+=stk[top][2];
top--;
}
stk[++top][1]=a[i];stk[top][2]=1+tmp;
}
while(top){
stk[top-1][2]+=stk[top][2];
ans=max(ans,(LL)stk[top][1]*stk[top][2]);
top--;
}
printf("%lld\n",ans);
}
return 0;
}