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