poj 2559 Largest Rectangle in a Histogram

传送门

解题思路

很久之前讲的题来填坑。。单调栈,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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值