zoukankan      html  css  js  c++  java
  • HDU 1506 Largest Rectangle in a Histogram

    和POJ 2796 Feel Good类似。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    const int maxn=100000+10;
    long long a[maxn];
    int L[maxn],R[maxn];
    int n;
    
    int main()
    {
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    
            for(int i=1;i<=n;i++) L[i]=i;
            for(int i=2;i<=n;i++)
            {
                if(a[i]>a[i-1]) continue;
                int pre=L[i-1];
                while(1)
                {
                    L[i]=pre;
                    if(pre==1||a[pre-1]<a[i]) break;
                    pre=L[pre-1];
                }
            }
    
            for(int i=n;i>=1;i--) R[i]=i;
            for(int i=n-1;i>=1;i--)
            {
                if(a[i]>a[i+1]) continue;
                int pre=R[i+1];
                while(1)
                {
                    R[i]=pre;
                    if(pre==n||a[pre+1]<a[i]) break;
                    pre=R[pre+1];
                }
            }
    
            long long ans=-1;
            for(int i=1;i<=n;i++)
               ans=max(ans,a[i]*(R[i]-L[i]+1));
    
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    进程和线程
    关于offer对比
    CVTE面经
    重定向
    奇虎360面试经验
    百纳信息(海豚浏览器)面经
    携程网面经
    百度面经
    位运算
    Cracking the Coding Interview 4.8
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5448096.html
Copyright © 2011-2022 走看看