zoukankan      html  css  js  c++  java
  • Lydsy2457 双端队列(双端队列思想)

    问题试求至少用几个双端队列可排序

    那么我们不妨换过来思考,将数列排好序后,再来找队列个数

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    struct my{
       int x,id;
    };
    
    const int maxn=200000+10;
    my a[maxn];
    int Max[maxn],Min[maxn];
    
    bool cmp(const my &c,const my &d){
         if(c.x==d.x) return c.id<d.id;
         return c.x<d.x;
    }
    
    int main(){
        int n;
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i].x);
            a[i].id=i;
        }
        int top=1;
        sort(a+1,a+1+n,cmp);
        Min[1]=a[1].id;
        a[0].id=a[1].id;
        for (int i=1;i<=n;i++){
            if(a[i].x!=a[i-1].x||i==1){
                Max[top]=a[i-1].id;
                Min[++top]=a[i].id;
            }
        }
        int ans=0,h=0x3f3f3f3f;
        bool b=0;
        for (int i=1;i<=top;i++){
            if(!b){
               if(Max[i]<h) h=Min[i];
               else {
                h=Max[i];
                b=1;
               }
            }
            else{
                if(Min[i]>h) h=Max[i];
                else{
                    ans++;
                    b=0;
                    h=Min[i];
                }
            }
        }
        printf("%d
    ",ans);
    return 0;
    }
  • 相关阅读:
    git
    HTML5 新增语义化标签
    vue directive 常用指令
    JS 数组 数组迭代方法 map, forEach, filter, some, every,
    图片居中
    进度条
    移动页面 REM自适应
    轮播图基本样式
    webpack3.0
    关于码云中项目提交的问题
  • 原文地址:https://www.cnblogs.com/lmjer/p/8975602.html
Copyright © 2011-2022 走看看