POJ2559 Largest Rectangle in a Histogram
Posted rax-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2559 Largest Rectangle in a Histogram相关的知识,希望对你有一定的参考价值。
Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26987 | Accepted: 8727 |
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.
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
****这道题是单调栈的题stact<intfloatdouble> st是建造一个栈,st是这个栈的名字,st.push(i)表示把i放进这个栈里面,st.top()栈顶元素,st.pop()删除栈顶元素,st.empty()判断这个栈是否为空,st.size()返回这个栈的大小。
复杂度o(n)
应用:
1.最基础的应用就是给定一组数,针对每个数,寻找它和它右边第一个比它大的数之间有多少个数。
2.给定一序列,寻找某一子序列,使得子序列中的最小值乘以子序列的长度最大。
3.给定一序列,寻找某一子序列,使得子序列中的最小值乘以子序列所有元素和最大。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<stack> 6 using namespace std; 7 int main() 8 { 9 int i,n,top;//top指向栈顶 10 stack<int> st;//栈用于保存矩形的编号,即位置 11 long long int tmp,ans,a[100010];//tmp为临时变量,记录面积的值,ans为结果,记录最大面积值 12 while(scanf("%d",&n) && n) 13 { 14 for(i = 1;i <= n;i++) 15 { 16 scanf("%lld",&a[i]); 17 } 18 ans = 0; 19 a[n + 1] = -1; //最后一个元素设为最小值,以最后清空栈 20 for(i = 1;i <= n + 1;i++) 21 { 22 if(st.empty() || a[i] >= a[st.top()]) 23 { //如果栈为空或入栈元素大于等于栈顶元素则入栈 24 st.push(i); 25 } 26 else 27 { 28 while(!st.empty() && a[i] < a[st.top()]) 29 {//如果栈非空且入栈元素小于栈顶元素,则将栈顶元素出栈 30 top = st.top(); 31 st.pop(); 32 tmp = (i - top) * a[top];//在出栈过程中计算面积值 33 if(tmp > ans)//更新面积最大值 34 { 35 ans = tmp; 36 } 37 } 38 st.push(top);//将这个值更新到最新的位置 39 a[top] = a[i]; 40 } 41 } 42 printf("%lld ",ans); 43 } 44 return 0; 45 }
以上是关于POJ2559 Largest Rectangle in a Histogram的主要内容,如果未能解决你的问题,请参考以下文章
POJ2559 Largest Rectangle in a Histogram
(单调栈)poj-2559 Largest Rectangle in a Histogram
POJ.2559Largest Rectangle in a Histogram(单调栈)
POJ.2559Largest Rectangle in a Histogram(单调栈)