最大长方形

最大长方形(二)

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

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.
输入
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectles 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.
输出
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.
样例输入
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
样例输出
8
4000
个人理解:考虑用单调队列的思路,先将所有高度的数据存入栈中,逐次进行比较,如果后面的数据小于前面的,将前面的数据压出栈中,并将对应的长度与单调匹配的高度与之相乘得到相应的面积,然后按照上面的思路将压出栈的面积比较,取最大面积进行输出处理。

#include<cstdio>
#include<iostream>
using namespace std;
int stack[100010]={-2},len[100010];
long long ans;
int main() {
	int n,top,h;
	while(scanf("%d",&n), n) {
		top=0;ans=0;
		for(int i=0;i<=n;i++) {
			if(i<n)scanf("%d",&h);
			else h=-1;
			if(h > stack[top]) {
				stack[++top]=h;
				len[top]=1;
			} else {
				int l = 0;
				while(stack[top] >= h) {
					ans=max(ans,(long long)(l+len[top])*stack[top]);
					l += len[top--];
				}
				stack[++top] = h;
				len[top] = l + 1;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


### 使用单调栈算法求解最大矩形面积 #### 直方图最大矩形面积分析 对于给定的一系列高度不同的直方图柱子,目标是从这些柱子中找到能够构成的最大矩形面积。这个问题可以通过单调栈方法高效解决。 考虑一个具体的例子:假设存在一组高度分别为 `2, 1, 5, 6, 2, 3` 的直方图柱子[^2]。为了计算这组柱子所能形成的最大矩形面积,可以采用单调栈的方法来进行处理。 #### 单调栈法过程 定义单调栈的行为(以单增栈为例): - **入栈**:当栈为空时,直接将当前元素压入栈;如果栈不为空,则比较栈顶元素与待插入元素大小关系。只有当栈顶元素严格小于新加入的元素时才允许其进入栈内;反之则不断弹出栈顶直至满足条件为止。 - **出栈、取栈顶、判栈空等操作均遵循常规栈的操作逻辑** 通过上述方式构建起来的就是所谓的“单调递增栈”。每当遇到一个新的柱体时,会尝试将其作为新的边界去扩展已有的矩形区域,并更新全局最优解。 具体来说,在遍历过程中维护这样一个性质良好的栈结构可以帮助快速定位到左侧最近的小于等于当前位置高度的位置以及右侧最接近的大于当前位置高度的地方,从而方便地算出以该位置为高的矩形可能达到的最大宽度并进一步得出面积[^3]。 #### 示例代码示例 以下是使用Python编写的基于单调栈求解直方图中最大矩形面积的函数实现: ```python def largestRectangleArea(heights): stack = [] max_area = i = 0 while i < len(heights): # 如果栈为空或当前高度大于栈顶索引对应的高度,则将当前索引入栈 if not stack or heights[i] >= heights[stack[-1]]: stack.append(i) i += 1 else: top = stack.pop() height = heights[top] width = i if not stack else (i - stack[-1] - 1) max_area = max(max_area, height * width) # 清理剩余未处理的栈内元素 while stack: top = stack.pop() height = heights[top] width = i if not stack else (i - stack[-1] - 1) max_area = max(max_area, height * width) return max_area ``` 这段程序首先初始化了一个空列表用于模拟栈的功能,并设置两个变量分别记录最终的结果(`max_area`)和当前正在考察的下标(`i`)。接着在一个循环体内交替执行两种主要动作之一—要么把符合条件的新节点推入栈底端(即增加),要么从顶端移除旧有成员的同时评估由它们界定出来的潜在候选方案的价值(即减少)—直到整个序列都被扫描完毕为止。最后一步则是针对那些仍然留在队列里的残留项做同样的事情,确保不会遗漏任何可能性[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值