最大矩形 —— 单调栈「建议收藏」

最大矩形 —— 单调栈「建议收藏」https://cn.vjudge.net/contest/245662#problemAhistogramisapolygoncomposedofasequenceofrectanglesalignedatacommonbaseline.Therectangleshaveequalwidthsbutmayhavedifferentheigh…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

https://cn.vjudge.net/contest/245662#problem

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.

题意:

给定从左到右多个矩形,已知这此矩形的宽度都为1,长度不完全相等。这些矩形相连排成一排,求在这些矩形包括的范围内能得到的面积最大的矩形,打印出该面积。所求矩形可以横跨多个矩形,但不能超出原有矩形所确定的范围。

思路:

       易证, 最终求得的最大矩形的高度一定是某个小矩形的高度。因此对于每一个高度求出它左右用这个高度可以覆盖到的左右两个位置,用单调栈来计算L[i]和R[i],相乘后输出最大值即可。

       对于每一个小矩形,它能覆盖到的最左面的位置是L[i],那么L[i]-1位置上的矩形高度一定不会高于或等于H[i](否则就一定可以向左扩展),最右面的位置是R[i],那么R[i]+1位置上的矩形高度一定不会高于H[i]。

       我们可以分开维护L[i]和R[i],首先对于L[i],如果栈为空则直接将H[i]入栈,L[i]为0(说明前面没有比H[i]更小的高度)。若栈非空且栈顶元素大于当前的高度H[i],则弹出栈顶元素(原因见上一段,而且当前弹出的元素对后面入栈的元素一定没有影响,因为栈顶元素比弹出的元素高度更小且更靠近后面入栈的元素),直到栈顶元素小于H[i]为止,然后将H[i]入栈。维护R[i]的时候同理(左右没有区别)。

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;

int st[100005], L[100005], R[100005], h[100005];

int main()
{
	int n;
	while (scanf("%d", &n)!=EOF){
		if (!n) break;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &h[i]);
		}

		int l=0; 
		for (int i = 1; i <= n ; i++)
		{
			while (l>0 && h[st[l]] >= h[i]) l--;
			if (l==0) L[i] = 0;
			else L[i] = st[l];
			l++;
			st[l] = i;
		}

		l=0;
		for (int i=n; i>=0; i--){
			R[i] = i+1;
			while (l>0 && h[st[l]] >= h[i])	l--;
			if (l==0) R[i] = n+1;  
			else R[i] = st[l];
			l++;
			st[l] = i;
		}

		long long ans=0;
		for (int i=1; i<=n; i++)
		{
			if (ans < (long long)h[i]*(R[i] - L[i] - 1))
				ans = (long long)h[i]*(R[i] - L[i] - 1);
		}

		printf("%lld\n", ans);
	}
	
	return 0;
}

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190688.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 类模板友元函数的声明(友元函数可以在类内定义吗)

    2009-08-09摘自《SunStudio12:C++用户指南》第6.7.3节 模板在使用前必须先声明。一个友元声明构成了模板的使用,而非模板的声明。(Afrienddeclarationconstitutesauseofthetemplate,notadeclarationofthetemplate.)所以实际的模板声明必须在友元声明之前

    2022年4月10日
    41
  • snmp协议详解-2_icmp报文封装在ip包的数据部分

    snmp协议详解-2_icmp报文封装在ip包的数据部分转自 https://blog.csdn.net/shmily_cml0603/article/details/12968157 一、什么是SNMP?SNMP是简单的网络管理协议,它不是一个软件,而是用于网络管理的一套规则。利用SNMP,一个管理工作站可以远程管理所有支持这种协议的网络设备,包括监视网络状态、修改网络设备配置、接收网络事件警告等。但话又说回来,为什么要使用SNMP协…

    2022年10月16日
    0
  • android之Fragment(官网资料翻译)[通俗易懂]

    Fragment要点Fragment作为Activity界面的一部分组成出现可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用。在Activity运行过程中,可以添加、移除或者替换Fragment(add()、remove()、replace())Fragment可以响应自己的输入事件,并且有自己的生命周

    2022年3月9日
    42
  • outlook显示与服务器断开连接,Outlook 与Exchange 服务器断开连接「建议收藏」

    outlook显示与服务器断开连接,Outlook 与Exchange 服务器断开连接「建议收藏」您好:我们使用的是Exchange2013标准版邮件服务器,上周升级为CU3后频繁出现outlook与exchange断开连接“outlook已失去与MicrosoftExchange的连接,如果有可能将尝试连接”……(当然之前也有只是不怎么影响使用,现在每分钟会出现55次,无法使用Exchange模式收发邮件)。下面截取了服务器日志和outlook日志,请帮忙分析下,不胜感激!!!服务器日志…

    2025年7月11日
    0
  • 网页显示400 bad request_1类错误拒绝无效假设

    网页显示400 bad request_1类错误拒绝无效假设在ajax请求后台数据时有时会报 HTTP400错误-请求无效(Badrequest);出现这个请求无效报错说明请求没有进入到后台服务里;原因:1)前端提交数据的字段名称或者是

    2022年8月1日
    5
  • 安装mysql-python报错_mac好用的ssh

    安装mysql-python报错_mac好用的sshmac系统安装mysqlclient时,会报错OSError:mysql_confignotfound解决办法在项目路径下输入以下内容PATH="$PATH":/usr

    2022年8月7日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号