HDU 1506 Largest Rectangle in a Histogram

来源:互联网 发布:java开发工作经历 编辑:程序博客网 时间:2024/05/18 20:36

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)   

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6735    Accepted Submission(s): 1911

Problem 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

View Code
 1 /* 2 (1) 3 lt[i],rt[i]分别存储的是第i个矩形的左边界和右边界,左边界也就是该边界到i的矩形的高度 4 均大于或等于矩形i,那么对于第i个矩形列入面积范围的话,最大面积为(rt[i]-lt[i]+1)*height[i]; 5 lt[i]和rt[i]均初始化为i,意思是一开始默认为只能到达自己 6 (2) 7 找左边界时,从自己的左边界找起,如果满足条件,则再找左边界的左边界,直到找到一个边界不满足高度 8 大小关系时结束。右边界也是如此。 9 最后枚举最大值10 11 78MS    2572K12 */13 14 #include <iostream>15 #include <cstdio>16 #include <cstring>17 #include <algorithm>18 #define SIZE 10000519 20 using namespace std;21 22 typedef __int64 Int;23 24 int n;25 Int height[SIZE];26 Int lt[SIZE],rt[SIZE];27 28 void init()29 {30     for(int i=0; i<SIZE; i++)31         lt[i] = rt[i] = i;32 }33 34 int main()35 {36     while(~scanf("%d",&n) && n)37     {38         for(int i=1; i<=n; i++)39             scanf("%I64d",&height[i]);40         init();41         height[0] = -1;42         height[n+1] = -1;43         for(int i=1; i<=n; i++)44         {45             while(height[lt[i]-1] >= height[i])46                 lt[i] = lt[lt[i]-1];47         }48         for(int i=n; i>=1; i--)49         {50             while(height[rt[i]+1] >= height[i])51                 rt[i] = rt[rt[i]+1] ;52         }53         Int ans = 0;54         for(int i=1; i<=n; i++)55             if(ans < (rt[i]-lt[i]+1)*height[i])56                 ans = (rt[i]-lt[i]+1)*height[i];57         printf("%I64d\n",ans);58     }59     return 0;60 }

 

原创粉丝点击