题目是英文的…这里简单描述一下,就是给你n个宽度1的矩形,然后连在一排,求新组成的图形中的最大矩形.
单调栈维护一个高度单调递增的栈,每个新数经来的时候要是比栈顶元素小的话就弹出栈顶元素直到不能再弹为止.过程中更新答案。最后清栈更新答案.
#include<stdio.h>
#include<algorithm>
#define dnt long long
using namespace std;
const int maxn=100005;
dnt ans,h[maxn],c[maxn],s[maxn],tmp;
int top,n;
inline const dnt read(){
register int f=1,x=0;
register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return f*x;
}
int main(){
while(n=read()){
ans=0,top=0;
for(register int i=1;i<=n;i++) h[i]=read();
for(register int i=1;i<=n;i++){
tmp=0;
while(top&&h[i]<=h[s[top]]){
ans=max(ans,h[s[top]]*(c[s[top]]+tmp));
tmp+=c[s[top]];
top--;
}
++top;
s[top]=i;
c[i]=tmp+1;
}
tmp=0;
while(top){
ans=max(ans,h[s[top]]*(c[s[top]]+tmp));
tmp+=c[s[top]];
top--;
}
printf("%lld\n",ans);
}
}