hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

# include <stdio.h>
# include <algorithm>
# include <iostream>
# include <math.h>
using namespace std;
__int64 a[100010],l[100010],r[100010];///l[i]左边连续大于等于a[i]的下标,r[i]右边连续大于等于a[i]的下标,所以对于a[i]的矩形面积为(l[i]-r[i]+1)*a[i];
int main()
{
    __int64 maxn;
    int i,n;
    while(~scanf("%d",&n),n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&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];
        }
        maxn=-1;
        for(i=1;i<=n;i++)
        {
            if(a[i]*(r[i]-l[i]+1)>maxn)
                maxn=a[i]*(r[i]-l[i]+1);
        }
        printf("%I64d\n",maxn);
    }
    return 0;
}

你可能感兴趣的:(HDU)