码迷,mamicode.com
首页 > 其他好文 > 详细

(单调栈)poj-2559 Largest Rectangle in a Histogram

时间:2017-03-25 23:52:10      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:lan   space   recommend   main   bsp   algo   const   har   div   

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, where0<=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.

单调栈的最入门题目。O(n)求一下l,r两个数组即可。

 1 #include<cstdio>
 2 #include <iostream>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <stack>
 6 typedef long long ll;
 7 typedef unsigned long long ull;
 8 #define pii pair <int, int>
 9 #define pll pair <ll, ll>
10 #define mp make_pair
11 using namespace std;
12 const int MAX=1e5+5;
13 int n;
14 int a[MAX],l[MAX],r[MAX];
15 stack <int> mono;
16 ll an;
17 int main()
18 {
19     while(scanf("%d",&n)&&n)
20     {
21         an=0;
22         for(int i=1;i<=n;i++)
23             scanf("%d",&a[i]);
24         while(!mono.empty())
25             mono.pop();
26         for(int i=1;i<=n;i++)
27         {
28             while(!mono.empty()&&a[mono.top()]>=a[i])
29                 mono.pop();
30             if(mono.empty())
31                 l[i]=0;
32             else
33                 l[i]=mono.top();
34             mono.push(i);
35         }
36         while(!mono.empty())
37             mono.pop();
38         for(int i=n;i>=1;i--)
39         {
40             while(!mono.empty()&&a[mono.top()]>=a[i])
41                 mono.pop();
42             if(mono.empty())
43                 r[i]=n+1;
44             else
45                 r[i]=mono.top();
46             mono.push(i);
47         }
48         for(int i=1;i<=n;i++)
49             an=max(an,(ll)((ll)r[i]-(ll)l[i]-1LL)*(ll)a[i]);
50         printf("%lld\n",an);
51     }
52     return 0;
53 }

 

(单调栈)poj-2559 Largest Rectangle in a Histogram

标签:lan   space   recommend   main   bsp   algo   const   har   div   

原文地址:http://www.cnblogs.com/quintessence/p/6618848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!