procon

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub mugen1337/procon

:heavy_check_mark: Largest Rectangle (最大長方形)
(DP/LargestRectangle.hpp)

概要

ヒストグラムH内に存在する最大長方形を求める.

計算量

O(|H|)

Verified with

Code

template<typename INT>
INT LargestRectangle(vector<INT> v){
    int n=(int)v.size();
    INT inf=numeric_limits<INT>::max()/2;
    stack<pair<INT,INT>> st;
    st.emplace(-inf,-1);// ばんぺいん
    INT ret=0;
    for(int i=0;i<n;i++){
        while(v[i]<st.top().first){
            auto p=st.top();st.pop();
            ret=max(ret,(i-st.top().second-1)*p.first);
        }
        st.emplace(v[i],i);
    }
    while(!st.empty()){
        auto p=st.top();st.pop();
        if(p.first==-inf) continue;
        ret=max(ret,(n-st.top().second-1)*p.first);
    }
    return ret;
}
#line 1 "DP/LargestRectangle.hpp"
template<typename INT>
INT LargestRectangle(vector<INT> v){
    int n=(int)v.size();
    INT inf=numeric_limits<INT>::max()/2;
    stack<pair<INT,INT>> st;
    st.emplace(-inf,-1);// ばんぺいん
    INT ret=0;
    for(int i=0;i<n;i++){
        while(v[i]<st.top().first){
            auto p=st.top();st.pop();
            ret=max(ret,(i-st.top().second-1)*p.first);
        }
        st.emplace(v[i],i);
    }
    while(!st.empty()){
        auto p=st.top();st.pop();
        if(p.first==-inf) continue;
        ret=max(ret,(n-st.top().second-1)*p.first);
    }
    return ret;
}
Back to top page