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

# include <cstdio>
# include <iostream>
# include <set>
# include <map>
# include <vector>
# include <list>
# include <queue>
# include <stack>
# include <cstring>
# include <string>
# include <cstdlib>
# include <cmath>
# include <algorithm>

# define ll long long
using namespace std ;

struct HEHE
{
    ll len , h ;
} ;

stack < HEHE > st ;

int main ( )
{
    cin . sync_with_stdio ( false ) ;
    int n ;
    st . push ( HEHE { 1 , 0 } ) ;
    while ( cin >> n , n )
    {
        st . top ( ) . len = 1 ;
        ll ans = 0 ;
        while ( n -- )
        {
            HEHE a ;
            cin >> a . h ;
            a . len = 1 ;
            while ( st . size ( ) > 1 && st . top ( ) . h > a . h )
            {
                HEHE t = st . top ( ) ;
                st . pop ( ) ;
                st . top ( ) . len += t . len ;
                ans = max ( ans , ( st . top ( ) . len - 1 ) * t . h ) ;
            }
            st . push ( a ) ;
        }
        while ( st . size ( ) > 1 )
        {
            HEHE t = st . top ( ) ;
            st . pop ( ) ;
            st . top ( ) . len += t . len ;
            ans = max ( ans , ( st . top ( ) . len - 1 ) * t . h ) ;
        }
        cout << ans << endl ;
    }
}

你可能感兴趣的:(POJ 2559 -- Largest Rectangle in a Histogram ( 单调栈 ))