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

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

#include 
#include 
#include
using namespace std;
#define ll long long
typedef pair pll;
stack 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;
}

 

你可能感兴趣的:(#,单调栈)