1 solutions

  • 0
    @ 2025-3-3 16:27:12

    C :

    #include<stdio.h>
    #define LL long long
    const LL inf  = 1e18;
    int que[100010];
    long long  height[100010];
    int main()
    {
    
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int top = 0;
            que[top] = n + 1;
            height[n + 1] =  inf;
            long long ans = 0;
            for(int i = 1;i <= n;i++) scanf("%lld",&height[i]);
    
            for(int i = n; i >= 1; i--)
            {
                while(top >= 0 && height[que[top]] < height[i]) top--;
                ans = ans + que[top] - i - 1;
                top++;
                que[top] = i;
            }
            printf("%lld\n",ans);
        }
        return 0;
    }
    
    

    C++ :

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    /*                    *\
         By Jormungand
    \*                    */
    
    int n;
    unsigned long long h[80010],f[80010],r[80010];
    
    int main()
    {
    
        scanf("%d",&n);
        for (int i=1; i<=n; i++)
            scanf("%d",&h[i]);
        
        for (int i=n; i>0; i--)
        {
            int j=i+1; 
            while (j<n+1 && h[i]>h[j]) j=r[j];
            r[i]=j;
            f[i]=j-i-1;
        }
        
        unsigned long long ans=0;
        for (int i=1; i<=n; i++)
            ans+=f[i];
        printf("%lld\n",ans);
    
    return 0;
    }
    
    
    • 1

    Information

    ID
    10154
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By