zoukankan      html  css  js  c++  java
  • 双端队列

    问题 F: 双端队列

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 27  解决: 11
    [提交] [状态] [命题人:admin]

    题目描述

    Sherry现在碰到了一个棘手的问题,有N个整数需要排序。
    Sherry手头能用的工具就是若干个双端队列。   
    她需要依次处理这N个数,对于每个数,Sherry能做以下两件事:
    1.新建一个双端队列,并将当前数作为这个队列中的唯一的数;
    2.将当前数放入已有的队列的头之前或者尾之后。
    对所有的数处理完成之后,Sherry将这些队列排序后就可以得到一个非降的序列。

    输入

    第一行包含一个整数N(N≤200000),表示整数的个数。接下来的N行每行包含一个整数Di,其中Di表示所需处理的整数。

    输出

    其中只包含一行,为Sherry最少需要的双端队列数。

    样例输入

    6
    3
    6
    0
    9
    6
    3
    

    样例输出

    2
    
    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn=2e5+5;
    pair<int,int>p[maxn];
    pair<int,int>q[maxn];
    int n,increase=1,cnt,pre=0x3f3f3f3f,ans;
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>q[i].first;
            q[i].second=i;
        }
        sort(q+1,q+1+n);
        for(int i=1;i<=n;i++){
            if(i==1||q[i].first!=q[i-1].first){
                p[cnt].second=q[i-1].second;
                p[++cnt].first=q[i].second;
            }
        }
        p[cnt].second=q[n].second;
        for(int i=1;i<=cnt;i++){
            if(increase){
                if(pre<p[i].first)pre=p[i].second;
                else pre=p[i].first,ans++,increase=0;
            }
            else{
                if(pre>p[i].second)pre=p[i].first;
                else pre=p[i].second,increase=1;
            }
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Codeforces467C George and Job
    Codeforces205E Little Elephant and Furik and RubikLittle Elephant and Furik and Rubik
    Codeforce205C Little Elephant and Interval
    51nod1829 函数
    51nod1574 排列转换
    nowcoder35B 小AA的数列
    Codeforce893E Counting Arrays
    gym101612 Consonant Fencity
    CodeForces559C Gerald and Giant Chess
    CodeForces456D A Lot of Games
  • 原文地址:https://www.cnblogs.com/czy-power/p/10564422.html
Copyright © 2011-2022 走看看