[POJ2559]Largest Rectangle in a Histogram(单调栈)

传送门


题解:
https://blog.csdn.net/qq1169091731/article/details/52006440

写的敲棒!


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=100010;
const int INF=1e9;
ll sta[N]; int top;
ll h[N],w[N];//S=h*w
int main()
{
    int n;
    while(scanf("%d",&n) && n)
    {
        for(int i=1;i<=n;i++) scanf("%d",&h[i]);
        h[n+1]=top=0; ll ans=0;
        for(int i=1;i<=n+1;i++)
        {
            if(h[i]>sta[top])
            {
                sta[++top]=h[i],w[top]=1;
            }
            else
            {
                int wi=0;
                while(h[i]<sta[top])
                {
                    wi+=w[top];
                    ans=max(ans,(ll)wi*sta[top]);
                    top--;
                }
                sta[++top]=h[i]; w[top]=wi+1;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cabi_zgx/article/details/80224861
今日推荐