您的位置:首页 > 其它

HDU 1506 Largest Rectangle in a Histogram

2012-03-14 17:54 423 查看
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:
View Code

#include<stdio.h>
#include<string.h>
int a[100001];
int r[100001];
int l[100001];
int main()
{
int i,n;
__int64 max,temp;
while(scanf("%d",&n)&&n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
l[i]=r[i]=i;
}
for(i=2;i<=n;i++)
{
while(l[i]>1&&a[l[i]-1]>=a[i])
l[i]=l[l[i]-1];
}
for(i=n-1;i>=1;i--)
{
while(r[i]<n&&a[r[i]+1]>=a[i])
r[i]=r[r[i]+1];
}
max=0;
for(i=1;i<=n;i++)
{
temp=(__int64)(r[i]-l[i]+1)*a[i];
if(max<temp)
max=temp;
}
printf("%I64d\n",max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: