ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SP1805 HISTOGRA - Largest Rectangle in a Histogram

2022-07-16 09:33:00  阅读:168  来源: 互联网

标签:矩形 long Histogram HISTOGRA SP1805 ans rec include rectangles


SP1805 HISTOGRA - Largest Rectangle in a Histogram

链接我就贴洛谷的吧:Luogu SP1805

题面翻译

题目描述

如图所示,在一条水平线上有 \(n\) 个宽为 \(1\) 的矩形,求包含于这些矩形的最大子矩形面积(图中的阴影部分的面积即所求答案)。

输入格式:

有多组测试数据,每组数据占一行。输入零时读入结束。

每行开头为一个数字 \(n(1\le n\le 10^5)\),接下来在同一行给出 \(n\) 个数字 \(h_1,h_2,\cdots, h_n (0\le hi\le 10^9)\),表示每个矩形的高度。

输出格式:

对于每组数据,输出最大子矩阵面积,一组数据输出一行。

题目描述

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.

输入格式

输出格式

样例 #1

样例输入 #1
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
样例输出 #1
8
4000

Solution

一道非常好的单调栈模板题(应该算是吧)。

根据题意,对于每一个矩形,它能取得的最大面积一定与它之前的出现过的更小的有关,因此可以维护一个单调栈。

插入新的矩形的时候,我们将栈中所有高度比当前矩形高的元素全部弹出,并且在过程中记录这些矩形面积的最大值来更新答案。因为这些元素高度比当前矩形高,就说明这些矩形的面积都不能对当前矩形具有贡献,因此需要将这些弹出。这一操作进行完后,栈要么是空栈,要么是栈顶元素小于等于当前矩形,那么前面弹出的所有矩形的宽之和就是现在这个矩形的宽(加上自己那个宽 \(1\) ),将这个矩形入栈,重复以上操作。

因为最后一个矩形处理完成后,很有可能栈中还停留有更大的矩形没有用来更新答案,所以就在 \(n+1\) 的位置额外添加一个高度为 \(0\) 的矩形,这样就可以利用这个矩形将所有矩形全部弹出来统计最大值。这是一个小技巧,很多题目都能用到。

另外一点,题目有多组测试数据,所以需要注意数据的初始化,本题中需要将栈清空并且将记录答案的 \(ans\) 清零,并且因为数据范围的缘故,需要开 \(\text{long long}\) 。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<stack>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
template<typename T> void read(T &k)
{
 	k=0;
	T flag=1;char b=getchar();
	while (b<'0' || b>'9') {flag=(b=='-')?-1:1;b=getchar();}
	while (b>='0' && b<='9') {k=(k<<3)+(k<<1)+(b^48);b=getchar();}
	k*=flag;
}
const int _SIZE=1e5;
int n;
int h[_SIZE+5];
stack<pair<int,int> > rec;
long long ans=0;
int main()
{
	while (true)
	{
		while (!rec.empty()) rec.pop();
		read(n);
		ans=0;
		if (n==0) return 0;
		for (int i=1;i<=n;i++) read(h[i]);
		n++,h[n]=0;
		for (int i=1;i<=n;i++)
		{
			int w=0;
			while (!rec.empty() && rec.top().first>h[i])
			{
				w+=rec.top().second;
				ans=max(ans,(long long)(w)*rec.top().first);
				rec.pop();
			}
			rec.push(make_pair(h[i],w+1));
		}
		printf("%lld\n",ans);
	}
}

标签:矩形,long,Histogram,HISTOGRA,SP1805,ans,rec,include,rectangles
来源: https://www.cnblogs.com/hanx16msgr/p/16483409.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有