Live2d Test Env

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;
}

 

posted @ 2018-03-25 10:26  nimphy  阅读(237)  评论(0编辑  收藏  举报