SPOJ.com - Problem HISTOGRA
- せっかくなんでブログの過去記事からひっぱってきた。
- スタックにつむときに 2 パターンあるのがポイント
class largest_rectangle_in_histogram {
public:
ll max_rectangle(const vector<ll> &_hist) {
vector<ll> hist(_hist);
stack<pair<int,ll>> st;
ll maxArea = 0;
hist.push_back(0);
REP(i, hist.size())
{
if (st.empty())
st.emplace(i, hist[i]);
else if (st.top().second == hist[i])
continue;
else
{
int prev = i;
while ( st.size() && hist[i] < st.top().second )
{
int l;
ll h;
tie(l,h) = st.top();
st.pop();
prev = l;
maxArea = max(maxArea, (i-l)*h);
}
st.emplace(prev, hist[i]);
}
}
return maxArea;
}
void solve(void) {
while (true)
{
int N;
cin>>N;
if (N == 0)
break;
vector<ll> hist;
REP(i, N)
{
ll h;
cin>>h;
hist.push_back(h);
}
cout<<max_rectangle(hist)<<endl;
}
}
};