HISTOGRA - Largest Rectangle in a Histogram

Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27122   Accepted: 8774

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

Hint

Huge input, scanf is recommended.

Source

如图所示,在一条水平线上有n个宽为1的矩形,求包含于这些矩形的最大子矩形面积(图中的阴影部分的面积即所求答案)。 输入格式: 有多组测试数据,每组数据占一行。输入零时读入结束。 每行开头为一个数字n(1<=n<=100000),接下来在同一行给出n个数字h1h2...hn(0<=hi<=1000000000)表示每个矩形的高度。 输出格式: 对于每组数据,输出最大子矩阵面积,一组数据输出一行。
 
 
作者: 冒泡ioa 更新时间: 2018-08-12 18:25  在Ta的博客查看  3 

部分内容来自李煜东所著《算法竞赛进阶指南》

如果说这道题矩形的高度是递增的,估计就是一道普及难度的题了吧。
我们可以尝试以每个矩形的高度作为最终矩形的高度,并将宽度扩展到右边界,答案取最大值。

简单归简单,不过这也给了我们一个启发,如果是递增的我们就放着不管,以后来处理。如果说下一个高度更小,那么用它所构成的矩形的高度不可能超过它自己,而后面的矩形想要和前面的矩形拼接的话,高度也不能超过它。
这样子的话,我们就可以用上面的方法更新比当前矩形高的矩形的答案再将它们合并。

这就是单调栈算法,时间复杂度 O(N)O(N)
借助单调性处理问题的思想在于及时排除不可能的选项,保持策略集合的高度有效性和秩序性

就这拿道题举例,我们建立一个栈,用来保存若干个矩形,这些矩形的高度是单调递增的,或者说,我们期望他是单调递增的。
我们从左到右读入矩形:
如果当前矩形比栈顶矩形高,即满足单调递增,进栈。
否则不断去除栈顶,直至栈空或栈顶高度低于当前矩形。在此过程中,我们累计被弹出的矩形的宽度和(用于计算答案与合并),用高度×累计宽度更新答案。而后,将一个宽度为累计宽度,高度为当前矩形的矩形入栈。
结束,将剩余矩形弹出,和上面一样更新答案;

 

这里的代码是记录这个矩形往左最远能到哪里

 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 const ll maxn=1e5+7;
 7 int h[maxn],stp;
 8 int n;
 9 ll ans;
10 struct Node{
11   int x,y;
12 }s[maxn];
13 int main(){
14   while(cin>>n&&n!=0){
15     for(int i=1;i<=n;i++) cin>>h[i];
16     h[++n]=-1;
17     s[0].x=0;s[0].y=-1;
18     stp=ans=0;
19     for(int i=1;i<=n;i++){
20       if(h[i]>=s[stp].y){
21         s[++stp].y=h[i];
22         s[stp].x=i;
23       }
24       else{
25         while(h[i]<s[stp].y){
26           ans=max(ans,1LL*s[stp].y*(i-1-s[stp].x+1));
27           stp--;
28         }
29         s[++stp].y=h[i];
30       }
31     }
32     cout<<ans<<endl;
33   }
34   return 0;
35 }

 

posted @ 2018-10-12 21:50  lcan  阅读(251)  评论(0编辑  收藏  举报