hdu 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5548    Accepted Submission(s): 1590


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 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 using namespace std;
 5 
 6 struct Node{
 7     __int64 right,left,num;
 8 };
 9 Node node[100005];
10 int main()
11 {
12     __int64 n,i,j,k;
13     
14     while(1)
15     {
16         scanf("%I64d",&n);
17         if(n==0) break;
18         for(i=1;i<=n;i++)
19         {
20             scanf("%I64d",&node[i].num);
21             node[i].left=i;            //初始化每个店的左右边界
22             node[i].right=i;
23         }
24         node[0].num=-1;
25         node[n+1].num=-1;
26         for(i=1;i<=n;i++)
27         {
28             while(node[i].num<=node[node[i].left-1].num)    //如果该点的高度比左边的一个小
29             {
30                 node[i].left=node[node[i].left-1].left;    //该点的左边界就是这个点左边的点的左边界
31             }
32         }
33         for(i=n;i>=1;i--)
34         {
35             while(node[i].num<=node[node[i].right+1].num)
36             {
37                 node[i].right=node[node[i].right+1].right;
38             }
39         }
40         __int64 ans=0;
41         __int64 temp;
42         for(i=1;i<=n;i++)
43         {
44             temp=node[i].num*(node[i].right-node[i].left+1);
45             if(ans<temp)
46                 ans=temp;
47         }
48         printf("%I64d\n",ans);
49     }
50     return 0;
51 }

 

 
posted on 2012-08-18 15:57  Zee、  阅读(1990)  评论(0编辑  收藏  举报