• 51nod 1215 数组的宽度


    若一个数在一段区间内作为最大值存在,那么答案应该加上这个数

    若一个数在一段区间内作为最小值存在,那么答案应该减去这个数

    所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定,加/减多少次a[i],最终得到答案

    #include<stdio.h>
    #include<math.h>
    #include<cstring>
    #include<stack>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #define MAXSIZE 100005
    #define LL long long
    
    using namespace std;
    const LL INF=1e19;
    
    LL a[MAXSIZE];
    int s[MAXSIZE];
    
    int main()
    {
        int n,top;
        LL ans = 0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        a[n+1] = 0;
        top = 0;
        for(int i=1;i<=n+1;i++)
        {
            if(top == 0)
            {
                s[++top] = i;
            }
    
            else
            {
                while(top>=1 && a[s[top]] > a[i])
                {
                    ans -= (i-s[top])*(s[top]-s[top-1])*a[s[top]];
                    top--;
                }
                s[++top] = i;
            }
        }
    
        top = 0;
        a[n+1] = INF;
        for(int i=1;i<=n+1;i++)
        {
            if(top == 0)
            {
                s[++top] = i;
            }
    
            else
            {
                while(top>=1 && a[s[top]] < a[i])
                {
                    ans += (i-s[top])*(s[top]-s[top-1])*a[s[top]];
                    top--;
                }
                s[++top] = i;
            }
        }
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    乐观锁和悲观锁
    c++ inline使函数实现可以在头文件中,避免多重定义错误
    无锁队列的实现
    同步异步阻塞非阻塞
    log(m+n)找第k大
    ios开发常见的 工具 - 值得收藏
    iOS 性能优化得 方法
    搭建 安卓开发环境 !
    项目支持 64 - bit
    AFN学习笔记
  • 原文地址:https://www.cnblogs.com/alan-W/p/8385053.html
Copyright © 2020-2023  润新知