largest Rectangle in a histogramA histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles has equal widths but could 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 is used to represent discrete distributions, e.g., the frequencies of characters in texts. Note the order of the rectangles, i.e, their heights, is important. Calculate the area of the largest rectangle in a histogram that's aligned at the common base line, too. The shows the largest aligned rectangle for the depicted histogram.
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer
n, denoting the number of rectangles it is composed of. Assume that
1<=n<=100000. Then follow
n integers
H1,..., hn, where
0<=hi<=1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is
1. A Zero follows the input for the last Test case.
Output
The For all test case is output on a single line, the area of the largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000
Sample Output
84000
Test instructions: From left to right there are n rectangles with a different height but 1 of the bottom edge, asking what the area of the rectangle from which the largest area can be cropped is. And the edge of the moment line must be parallel to the longitudinal axis and horizontal axis.
Parse: Set two array le[],ri[], respectively, a small rectangle as the starting point, left and right can extend the farthest position (smaller than the height of the small rectangle) with a monotonic stack maintenance, I wrote the le[],ri[] is the first non-qualifying position, so the area =h[i]* (ri[i]-le[ I]-1);
The code is as follows:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<set>#include<map>#include<queue>#include<vector>#include<iterator>#include<utility>#include<sstream>#include<iostream>#include<cmath>#include<stack>UsingNamespace Std;Constint INF=1000000007;ConstDouble EPS=0.00000001;typedef __Int64LL;LLH[100005],le[100005],ri[100005],que[100005]; Height, left border, right boundary, monotone stackIntMain(){int N;While(CIN>>n){If(! N)Break;For(int I=1; I<=n; I++)scanf("%i64d", &h[I]);int rear=0;For(Int St=1; St<=n; St+ +)//swipe from left to right, find le[]{While(Rear>0&&h[Que[Rear]]>=h[St])--rear; UpdateIf(Rear==0) Le[St]=0; If the stack is empty, the boundary is set to 0.Else Le[St]=que[Rear]; Que[++rear]=st; Into the stack} rear=0;For(Int St=n; St>=1; St--)//swipe from right to left{While(Rear>0&&h[Que[Rear]]>=h[St])--rear;If(Rear==0) RI[St]=n+1;Else RI[St]=que[Rear]; Que[++rear]=st;}LLAns=0;For(int I=1; I<=n; I++)if (Ans[i ]* (Ri[i]-le[i< Span class= "Sh-symbol" >]-1=H[i]* (Ri[i]-le[i]-1);//Answer Cout<<ans<<endl return 0;< Span class= "Sh-cbracket" >
Hdu 1506 largest Rectangle in a histogram (monotonic stack)