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;
    }
  • 相关阅读:
    Spring框架 基础01
    Mybatis框架 基础
    字节流,字符流
    集合的应用(练习:学生档案)
    集合
    时间类型的格式化(字符串和时间类型 之间的相互转换)
    逢三退一(boolean数组的使用)
    电子宠物(线程,实现方法)
    点是否在圆里
    sqlserver 指定上月25-本单据日期/本月24 数据汇总的保存后存储过程
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8643283.html
Copyright © 2011-2022 走看看