POJ 2559 Largest Rectangle in a Histogram (栈的运用)
Posted hinata_hajime
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2559 Largest Rectangle in a Histogram (栈的运用)相关的知识,希望对你有一定的参考价值。
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.Input
Output
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
Hint
#include <cstdio> #include <iostream> #include <cstring> #include <map> #include <algorithm> #include <stack> using namespace std; typedef long long ll; const int MAXN=1e5+10; ll a[MAXN]; ll L[MAXN]; ll R[MAXN]; int main() { int n; ll maxx; while(scanf("%d",&n)!=EOF&&n) { maxx=0; stack<ll>S; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); L[1]=0; S.push(1); for(ll i=2;i<=n;i++) { while(!S.empty()&&a[S.top()]>=a[i]) S.pop(); if(S.empty()) L[i]=0; else { // cout<<" "<<S.top()<<endl; L[i]=S.top(); } // printf("%d\n",L[i]); S.push(i); } while(!S.empty()) S.pop(); R[n]=n+1; S.push(n); for(int i=n-1;i>=1;i--) { while(!S.empty()&&a[S.top()]>=a[i]) S.pop(); if(S.empty()) R[i]=n+1; else { R[i]=S.top(); } S.push(i); } for(int i=1;i<=n;i++) { maxx=max(maxx,(R[i]-L[i]-1)*a[i]); } printf("%lld\n",maxx); } return 0; }
以上是关于POJ 2559 Largest Rectangle in a Histogram (栈的运用)的主要内容,如果未能解决你的问题,请参考以下文章
POJ2559 Largest Rectangle in a Histogram
(单调栈)poj-2559 Largest Rectangle in a Histogram
POJ.2559Largest Rectangle in a Histogram(单调栈)
POJ.2559Largest Rectangle in a Histogram(单调栈)