zoukankan      html  css  js  c++  java
  • BZOJ2761:不重复数字(splay效率对比)

    给出N个数,要求把其中重复的去掉,只保留第一次出现的数。
    例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。
     
    Input
    输入第一行为正整数T,表示有T组数据。
    接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。
     
    Output
     
    对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。
    Sample Input
       2
       11
       1 2 18 3 3 19 2 3 6 5 4
       6
       1 2 3 4 5 6

    Sample Output

       1 2 18 3 19 6 5 4

      1 2 3 4 5 6

    map:1108ms

    #include<map>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    map<int ,int >mp;
    int main()
    {
        int T,N,i,x,ans;
        scanf("%d",&T);
        while(T--){
            mp.clear();
            scanf("%d",&N);
            while(N--){
                scanf("%d",&x);
                if(mp.find(x)==mp.end()) printf("%d ",x);
                mp[x]=1;
            }
        }
        return 0;
    }
    View Code

    二叉树:764ms

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=50010;
    struct Splay
    {
        int ch[maxn][2],fa[maxn],key[maxn],rt,cnt;
        void init(){
            rt=cnt=0;
        }
        int get(int x) { return ch[fa[x]][1]==x; }
        bool insert(int x)
        {
            int Now=rt,f=0;
            while(Now){
                if(key[Now]==x){
                    //splay(Now,0);
                    return true;
                }
                f=Now;
                Now=ch[Now][key[Now]<x];
            }
            if(!rt){
                rt=++cnt; key[cnt]=x;  fa[cnt]=0; 
                ch[cnt][0]=ch[cnt][1]=0;
                return false;
            }
            key[++cnt]=x;
            fa[cnt]=f; ch[f][key[f]<x]=cnt;
            ch[cnt][0]=ch[cnt][1]=0;
            //splay(cnt,0);
            return false;
        }
        void rotate(int x)
        {
            int old=fa[x],fold=fa[old],opt=get(x);
            ch[old][opt]=ch[x][opt^1]; fa[ch[x][opt^1]]=old;
            ch[x][opt^1]=old; fa[old]=x;
            ch[fold][get(old)]=x,fa[x]=fold;
        }
        void splay(int x,int y)
        {
            for(int f;(f=fa[x])!=y;rotate(x))
              if(fa[f]!=y) 
               rotate(get(f)==get(x)?f:x);
            if(!y) rt=x;
        }
    }S;
    int main()
    {
        int T,N,i,x,ans;
        scanf("%d",&T);
        while(T--){
            S.init();
            scanf("%d",&N);
            while(N--){
                scanf("%d",&x);
                if(!S.insert(x)) printf("%d ",x);
            }
        }
        return 0;
    }
    View Code

    splay:1208ms

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=50010;
    struct Splay
    {
        int ch[maxn][2],fa[maxn],key[maxn],rt,cnt;
        void init(){
            rt=cnt=0;
        }
        int get(int x) { return ch[fa[x]][1]==x; }
        bool insert(int x)
        {
            int Now=rt,f=0;
            while(Now){
                if(key[Now]==x){
                    splay(Now,0);
                    return true;
                }
                f=Now;
                Now=ch[Now][key[Now]<x];
            }
            if(!rt){
                rt=++cnt; key[cnt]=x;  fa[cnt]=0; 
                ch[cnt][0]=ch[cnt][1]=0;
                return false;
            }
            key[++cnt]=x;
            fa[cnt]=f; ch[f][key[f]<x]=cnt;
            ch[cnt][0]=ch[cnt][1]=0;
            splay(cnt,0);
            return false;
        }
        void rotate(int x)
        {
            int old=fa[x],fold=fa[old],opt=get(x);
            ch[old][opt]=ch[x][opt^1]; fa[ch[x][opt^1]]=old;
            ch[x][opt^1]=old; fa[old]=x; fa[x]=fold;
            if(fold) ch[fold][ch[fold][1]==old]=x;
        }
        void splay(int x,int y)
        {
            for(int f;(f=fa[x])!=y;rotate(x))
              if(fa[f]!=y) 
               rotate(get(f)==get(x)?f:x);
            if(!y) rt=x;
        }
    }S;
    int main()
    {
        int T,N,i,x,ans;
        scanf("%d",&T);
        while(T--){
            S.init();
            scanf("%d",&N);
            while(N--){
                scanf("%d",&x);
                if(!S.insert(x)) printf("%d ",x);
            }
        }
        return 0;
    }
  • 相关阅读:
    JVM参数默认值列表
    垃圾回收G1日志解析
    《深入理解JAVA虚拟机》垃圾回收时为什么会停顿
    《深入理解JAVA虚拟机》JDK的垃圾收集算法
    什么才是技术?
    Lodash使用示例(比较全)
    MSCL超级工具类(C#),开发人员必备,开发利器
    刷新SqlServer数据库中所有的视图
    Sql Server 2014/2012/2008/2005 数据库还原出现 3154错误的解决办法
    C#中执行批处理文件(.bat),执行数据库相关操作
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8643283.html
Copyright © 2011-2022 走看看