Largest Rectangle in a Histogram POJ - 2559 (单调栈)

版权声明:本文为博主瞎写的,请随便转载 https://blog.csdn.net/sdut_jk17_zhangming/article/details/85235379

https://cn.vjudge.net/problem/POJ-2559

#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define ll long long
typedef pair<ll,ll> pll;
stack<pll> s;
int main()
{
    ll n,m,i,j,ans,x;
    while(scanf("%lld",&n) != EOF&&n){
    ll MAX = 0;
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        ll w = 0;
        while(!s.empty()&&s.top().first >= x)
        {
            ll tmph = s.top().first;
            ll tmpw = s.top().second;
            s.pop();
            w += tmpw;
            MAX = max(MAX,tmph*w);
        }
        s.push(make_pair(x,w+1));
    }
    int tmp = 0;
    while(!s.empty())
    {
        MAX = max(MAX,s.top().first*(tmp+s.top().second));
        tmp += s.top().second;
        s.pop();
    }
    printf("%lld\n",MAX);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdut_jk17_zhangming/article/details/85235379