【HDU.2559】Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram

直方图中最大的矩形

Language:
Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36046 Accepted: 11781
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:
【HDU.2559】Largest Rectangle in a Histogram(单调栈)_第1张图片

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.

直方图是一个在一个共同的基线上对齐的矩形序列组成的多边形。 矩形的宽度相等,但高度可能不同。 例如,左边的图表显示了直方图,由高度为2,1,4,5,1,3,3的矩形组成,以1为矩形的宽度单位测量:
【HDU.2559】Largest Rectangle in a Histogram(单调栈)_第2张图片

通常,直方图用来表示离散的分布,例如,文本中字符的频率。 注意矩形的顺序,也就是它们的高度,很重要。 还可以计算直方图中与公共基线对齐的最大矩形的面积。 右边的图显示了直方图中最大的对齐矩形。
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.

输入包含几个测试用例。 每个测试用例描述一个直方图,并以整数 n 开始,表示它组成的矩形的数量。 你可以假设1 n 100000。 然后跟随 n 个整数 h 1,… ,hn,其中0 hi 1000000000。 这些数字按照从左到右的顺序,表示直方图矩形的高度。 每个矩形的宽度为1。 最后一个测试用例的输入后跟着一个零。
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

解题思路

建立一个单调递增栈,所有元素各进栈和出栈一次即可。
每个元素出栈的时候更新最大的矩形面积

注意:要用scanf和printf,不然会超时

AC代码

#include
#include
using namespace std;
long long n,o,s,tail,a[100005];
struct node
{
	long long x,y;
}f[100005];
int main()
{
	scanf("%lld",&n);
    while(n!=0)
     {
     	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
     	s=tail=0;//初值
     	for(int i=1;i<=n;i++)//进栈
     	 {
     	 	o=0;
     	 	while(f[tail-1].x>=a[i]&&tail>0)//弹出
     	 	 {
     	 	   s=max(s,f[tail-1].x*(f[tail-1].y+o));//出栈的要计算
     	 	   o+=f[tail-1].y; //累加
     	 	   tail--;
     	 	 }
     	 	f[tail].x=a[i];//进栈
     	 	f[tail].y=o+1;
     	 	tail++;
     	 }
     	o=0;
     	while(tail>0)//出栈
     	 {
     	    s=max(s,f[tail-1].x*(f[tail-1].y+o));//出栈的要计算
     	 	o+=f[tail-1].y;//累加
     	 	tail--;
     	 }
     	printf("%lld\n",s);//输出
     	scanf("%lld",&n);
     }
}

谢谢

你可能感兴趣的:(单调栈)