• BZOJ 2457 双端队列(思维


    2457: [BeiJing2011]双端队列

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 582  Solved: 253
    [Submit][Status][Discuss]

    Description

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

    Input

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

    Output

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

    Sample Input


    6
    3
    6
    0
    9
    6
    3

    Sample Output


    2

    HINT

    100%的数据中N≤200000。

     
     
    代码如下:
    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int maxn = 2e5+5;
    struct node{
        int x;
        int id;
    }a[maxn];
    bool cmp(node a,node b){
        if(a.x==b.x){
            return a.id<b.id;
        }
        return a.x<b.x;
    }
     
    int cnt;
    int ans;
    int Max[maxn];
    int Min[maxn];
     
    int main(){
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        int n;
        cnt=ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].x);
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp);
        //分成相等的块
        for(int i=1;i<=n;i++){
            if(a[i].x!=a[i-1].x||i==1){
                Max[cnt]=a[i-1].id;
                Min[++cnt]=a[i].id;
            }
        }
        Max[cnt]=a[n].id;
        //for(int i=1;i<=cnt;i++){
        //  cout<<Max[i]<<" ";
        //}
        //cout<<endl;
        //for(int i=1;i<=cnt;i++){
        //  cout<<Min[i]<<" ";
        //}
        //cout<<endl;
        int h=99999999;
        int flag=1;
        //flag表示当前的波的走势
        for(int i=1;i<=cnt;i++){
            if(!flag){
                if(h>Max[i]) h=Min[i];   //这里是递增的序
                else h=Max[i],flag=1;   //更新到谷峰
            }else{
                if(h<Min[i]) h=Max[i];
                else ans++,h=Min[i],flag=0;  //谷底
                //找不同的块,这里不同 就++  更新h
            }
            //cout<<flag<<" "<<h<<endl;
        }
        //cout<<endl;
        printf("%d
    ",ans);
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    c#读sql server数据添加到MySQL数据库
    ASP.NET取得Request URL的各个部分
    jquery,attr,prop,checkbox标签已有checked=checked但是不显示勾选
    ASP.NET 4.0 :MasterPage母版页的ClientIDMode属性
    百度地图 根据坐标,获取省份,城市,区域
    ECharts
    SQL 更新修改删除一个表,库存自动增减的写法
    ajaxFileupload 多文件上传
    JSON C# Class Generator ---由json字符串生成C#实体类的工具
    mvc 部署到iis 提示错误未能加载文件或程序集System.Web.Http.WebHost
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9452973.html
Copyright © 2020-2023  润新知