POJ ——1056 —— Largest Rectangle in a Histogram

Problem 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

柱状图是一个多边形,由一系列在公共基线上对齐的矩形组成。这个 矩形的宽度相等,但高度不同。例如,左边的图显示
 以单位度量的高度为2、1、4、5、1、3、3的矩形组成的柱状图。 其中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.
通常,柱状图用于表示离散分布,例如字符的频率 在文本中。请注意,矩形的顺序,即它们的高度,非常重要。计算柱状图中
最大矩形的区域,也与公共基线对齐。这个右图显示了所示柱状图的最大对齐矩形。

本题为单调栈的应用:对于每个点对应最大面积的矩形的左右端点用单调栈来维护,再遍历每个点对应的找出最大矩形的面积值。

#include"iostream"
#define MaxSize 1000000+4
using namespace std;
long long H[MaxSize],D[MaxSize],L[MaxSize],R[MaxSize],s[MaxSize],sum[MaxSize],ans,N,top;
int main()
{
     
 while(~scanf("%d",&N)&&N) 
 {
 for(int i = 1;i <= N;i++)
 cin >> H[i];
 top = 0;
for(int i = 1;i <= N;i++){
while(top!=0&&H[s[top]]>=H[i])top--;
{
if(top==0)L[i] = 0;
else L[i] = s[top];
s[++top] = i;
}
}
top = 0;
for(int i = N;i>=1;i--){
while(top!=0&&H[s[top]]>=H[i])top--;
{
if(top==0)R[i] = N;
else R[i] = s[top]-1;
s[++top] = i ;
}
}
ans = 0;
for(int i = 1;i <= N;i++)
{ 
ans = max(ans,(R[i]-L[i])*H[i]); 
}
cout << ans<<endl;
}
 return 0;
}

要是觉得该题是英文题面看着不爽可以戳这个链接牛客小白月赛13—H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值