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 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.

自动化学霸宗学长除了写代码之外就是研究单片机小车,他平时最大的爱好就是到某宝上网购各种各样的小车零件,终于有一天,宗学长组装好了N辆小车,宗把他们依次叫做GZ1, GZ2到CZn。为了想他的好朋友们展示他的成果,宗学长把他的N辆小车放在同一起跑线上启动,开始展示它们的性能,然而,N辆小车在前进了若干距离之后纷纷抛锚gg。但是,宗学长非要说他有更大的研究课题,N辆小车并排在一起,每辆小车的宽度都是1,N辆小车走过的路径组成N个并排的矩形,给出每辆小车i前进的距离ai,他要在他的小车走过的地方画出一个尽可能大的矩形。但是,宗学长要去修他的小车,于是他把这个问题抛给了你,机智的你能帮学长求出这个最大矩形的面积吗?

Input

多组用例,每组用例一行。每个用例开始是一个整数N,表示小车的数量,接下来是n个正整数, 表示每辆小车前进的距离。n=0表示用例结束。(1 <= N <= 1e5, 0 < ai < 1e9)

Output

对于每组用例输出一行一个整数,表示最大的矩形的面积。

关于这个题写了两遍训练赛又错了,感觉很有必要记录一下这道题目;

题目大义如上:就是求一个连续的长方形的最大面积(所以最长的长方形的高度一定最小,最大的长方形的面积一定最大)这样考虑长方形的长度是否能拓展的时候就有了一点单调栈的味道;

现在给你一串数字代表各个长方形的高度

2 1 4 5 1 3 3

然后说一下我的思路:首先栈是空的所以可以直接将2入栈,因为2可以拓展,只要有能拓展的可能都可入栈;然后因此只要比栈顶大的元素都可以入栈,他们的宽度都为1;然后读到第二个数1;此时2已经没有拓展的可能了;索性记录它此时的面积(ans=max(ans,cnt*st[top]);)然后向下拓展(此时就要将1入栈因为它可以向后拓展);然后读入4,5,宽度是1;此时又一次读到了比4,5都小的1,然后就记录他们的面积然后向后拓展,然后以此类推,然后这里面有一个小细节就是让我们读入数组的最后一位的下一位变为零然后它再回来记录面积的话可以记录每一个在栈内的面积;这样我们就找到了整张图的最大连续面积;

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
long long st[N],w[N], x[N];long long ans, top;
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
		return 0;
		for(int i=1;i<=n;i++)
			cin>>x[i];
		memset(w,0,sizeof(w));
		x[n+1]=0;
		ans=0;top=0;
		for(int i=1;i<=n+1;i++)
		{
			if(x[i]>st[top])
			{
				st[++top]=x[i];
				w[top]=1;
			}
			else
			{
				long long cnt=0;
				while(x[i]<st[top])
				{
				cnt+=w[top];
				ans=max(ans,cnt*st[top]);
				top--;	
				}
				st[++top]=x[i];
				w[top]=cnt+1;
			}
		}
		cout<<ans<<endl;
	} 
}

其中还有几个长方形高度可能相等的情况看似没考虑其实包含在了里面,长度相等可以入栈毕竟有拓展的可能,也不影响结果因为最后一个比较的是零所以不会影响结果;

一定要明白的事情是如果当前的元素比栈顶的元素小的话栈顶元素就没有拓展的可能了

若有表述错误还请大佬指正;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值