1 条题解

  • 0
    @ 2025-10-8 16:50:17
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=1e5+10;
    
    struct node{LL d,w;};
    LL a[N];
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF && n)
        {
            for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
            a[n+1]=0;
            
            LL ans=0;
            stack<node> stk;
            
            for(int i=1;i<=n+1;i++)
            {
                LL w=0;
                while( stk.size() && a[i]<stk.top().d )
                {
                    w+=stk.top().w;
                    ans=max(ans, stk.top().d  *  w     );
                    stk.pop();
                }
                stk.push( node{a[i],w+1} ) ;
            }
            printf("%lld\n",ans);
        }
        return 0;
    }
    

    刘志源代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N=1e5+10;
    int a[N],l[N],r[N],n;
    ll ans;
    int main()
    {
        a[0]=-1;
        while(scanf("%d",&n),n)
        {
            for(int i=1;i<=n;i++)scanf("%d",&a[i]),l[i]=r[i]=i;
            a[n+1]=ans=-1;
            for(int i=2;i<=n;i++)
                while(a[i]<=a[l[i]-1])l[i]=l[l[i]-1];
            for(int i=n-1;i>=1;i--)
                while(a[i]<=a[r[i]+1])r[i]=r[r[i]+1];
            for(int i=1;i<=n;i++)ans=max(ans,(ll)a[i]*(r[i]-l[i]+1));
            printf("%lld\n",ans);
        }
        return 0;
    }
    
    • 1

    *【栈:单调栈】直方图的最大子矩阵面积

    信息

    ID
    388
    时间
    1000ms
    内存
    128MiB
    难度
    7
    标签
    递交数
    272
    已通过
    57
    上传者