POJ 2559 Largest Rectangle in a Histogram

Description

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

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. You may assume that  1<=n<=100000. Then follow  n integers  h1,...,hn, where  0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is  1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

 

Sample output

8

4000

translation

A histogram is a polygon, a series of aligned on common baseline rectangles. Equal to the width of the rectangle, but different heights. For example, the left side shows a histogram of the height of the rectangles 2,1,4,5,1,3,3, in metric units, where 1 is the width of the rectangle:
Typically, a histogram representing discrete distribution, such as the frequency of characters in the text. Notice that a rectangle in the order that their height is very important. Calculating the maximum rectangular area histogram, the histogram is also aligned with the common baseline. The right panel shows the maximum histogram shown aligned rectangle.
Input
Input consists of several test cases. Each test case is described a histogram, beginning with an integer n, the number of which it is composed of a rectangle. You can assume that 1 <= n <= 100000. Then follows the n integers h1, ..., hn, where 0 <= hi <= 100000000. These numbers represent the height of the rectangle of the histogram from left to right. The width of each rectangle is 1. Behind entering the last test case is followed by a zero.
Output
for each test case on a single line output, histograms designated largest rectangle. Remember, this rectangle must be aligned with the common baseline.

analysis

This question is to use the Internet mostly monotonous stack, but I think the problem in a monotone and monotonous queue stacks are the same

This is definitely a rectangle extended to both sides as a benchmark

We can to a certain height of the pillars of the standard, as far as possible extensions to the two, so that you can find in its height as a standard, and contains the largest rectangle itself. Then for each column do similar work, which finally pick the largest rectangles.

Is there a way to move with the regulations inside the largest sub-matrices like?

Finally, attention to detail

Code

 1 /************************
 2 User:Mandy.H.Y
 3 Language:c++
 4 Problem:POj 2559
 5 Algorithm: 
 6 ************************/
 7 //#include<bits/stdc++.h>
 8 #include<cstdio>
 9 #include<iomanip> 
10 #include<cmath> 
11 
12 using namespace std;
13 
14 const int maxn = 1e5 + 5;
15 
16 int n,l,r;
17 int q[maxn];
18 
19 struct Node{
20     int h,l,r;
21 }node[maxn]; 
22 
23 template<class T>inline void read(T &x){
24     x = 0;bool flag = 0;char ch = getchar();
25     while(!isdigit(ch)) flag |= ch == '-',ch =  getchar();
26     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch =  getchar();
27     if(flag) x = -x;
28 }
29 
30 template<class T>void putch(const T x){
31     if(x > 9) putch(x / 10);
32     putchar(x % 10 | 48);
33 }
34 
35 template<class T>void put(const T x){
36     if(x < 0) putchar('-'),putch(-x);
37     else putch(x);
38 }
39 
40 void file(){
41     freopen("2559.in","r",stdin);
42 //    freopen("1090.out","r",stdin);
43 }
44 
45 void work(){
46     read(n);
47     while(n){
48         long long ans = 0;
49         for(int i = 1;i <= n; ++ i) read(node[i].h),node[i].l = i,node[i].r = i;
50          // initialize L, R & lt 
51 is          L = R & lt = 0 ;
 52 is          for ( int I = . 1 ++;; I <= n- {I)
 53 is              the while - (L <R & lt && Node [Q [R & lt . 1 . H]] > = Node [I] .h) Node [I] .l Node = [Q [R & lt - . 1 ]] L, r--. ;
 54 is              // Q is stored in the ID, node [i] .l updates . = node [q [r - 1]] l, +1 coordinates that may be remaining, empty but to sentence 
55              Q [R & lt ++] = I;
 56 is          }
 57 is          L = R & lt = 0 ;
 58          for ( int I n-=; I> = . 1 ; - i){
59             while(l < r && node[q[r - 1]].h >= node[i].h) node[i].r = node[q[r - 1]].r,r--;
60             q[r++] = i;
61             ans = max(ans,(long long)node[i].h * (node[i].r - node[i].l + 1));
62         }
63         put(ans);
64         putchar('\n');
65         read(n);
66     }
67 }
68 
69 int main(){
70 //    file();
71     work();
72     return 0;
73 }
View Code

 

Guess you like

Origin www.cnblogs.com/Mandy-H-Y/p/11420282.html