poj2559 Largest Rectangle in a Histogram

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

这题之前是用dp做的,现在用单调栈做了一下。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100500
struct node{
	ll count,h;
}stack[maxn];
int main()
{
	int n,m,i,j;
	ll ans,sum1,top,count,tmp,a;
	while(scanf("%d",&n)!=EOF && n!=0)
	{
		top=0;ans=0;
		for(i=1;i<=n;i++){
			scanf("%lld",&a);
			count=0;
			while(top>0 && stack[top].h>=a){
				stack[top].count+=count;
				count=stack[top].count;
				sum1=count*stack[top].h;
				if(ans<sum1)ans=sum1;
				top--;
			}
			top++;
			stack[top].h=a;
			stack[top].count=count+1;
		}
		count=0;
		while(top>0){
				stack[top].count+=count;
				count=stack[top].count;
				sum1=count*stack[top].h;
				if(ans<sum1)ans=sum1;
				top--;
	    }
	    printf("%lld\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 12:53:35

poj2559 Largest Rectangle in a Histogram的相关文章

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

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

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

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ2559 Largest Rectangle in a Histogram (单调栈

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012   Accepted: 8416 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ2559 Largest Rectangle in a Histogram(单调栈)

题目给一个由几个相连接的矩形组成的多边形,计算多边形包含的最大的矩形的面积. 要求的矩形的高一定是某一个用来组合的矩形的高:如果枚举每个矩形作为高的话,那样长就是这个矩形能向左向右继续延伸矩形的长度了. 所以这题本质也是用单调栈在O(n)计算出每个数作为最小数向左和向右能延伸的最长距离. 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define MAXN 111111 5 int a[MAXN

POJ2559 Largest Rectangle in a Histogram 单调队列

题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于等于另一个小矩形的高. 我们现考虑面积最大矩形左边高等于其所在小矩形的高的情况,则其右边高小于等于其对应的小矩形的高(以后将此简称为左等高矩形).我们要维护一个单调栈,使得栈里的矩形的高度都是单调递增的.一个一个枚举小矩形,如果当前小矩形高度比栈顶的矩形高度高或等,则如果所求大矩形是个左边为栈内矩形

POJ2559——DP——Largest Rectangle in a Histogram

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 rectang

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

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

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

POJ 2559 Largest Rectangle in a Histogram(单调栈)

[题目链接]:click here~~ [题目大意]: 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 con