InputThe 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.OutputFor 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
分析:找出左边比它大的和右边比他大的
#include<stdio.h>
long long m[100010];
long long add[100010],l[100010],r[100010];
int main()
{
int n;
scanf("%d",&n);
while(n!=0)
{
long long s=0,flag;
for(int i=1;i<=n;i++)
scanf("%I64d",&m[i]);
l[1]=1;
r[n]=n;
for(int i=2;i<=n;i++)
{
int tt=i;
while(tt>1&&m[i]<=m[tt-1])tt=l[tt-1];
l[i]=tt;
}
for(int i=n-1;i>=1;i--)
{
int tt=i;
while(tt<n&&m[i]<=m[tt+1])tt=r[tt+1];
r[i]=tt;
}
for(int i=1;i<=n;i++)
{
flag=(r[i]-l[i]+1)*m[i];
if(s<flag)
s=flag;
}
printf("%I64d\n",s);
scanf("%d",&n);
}
return 0;
}
原文:http://www.cnblogs.com/xzxj/p/6597066.html