POJ2104 最大矩形面积 单调栈

1 篇文章 0 订阅

题目是英文的…这里简单描述一下,就是给你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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值