【BZOJ2761/JLOI2011】不重复数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38083668/article/details/82940563

                                2761: [JLOI2011]不重复数字

                                                Time Limit: 10 Sec  Memory Limit: 128 MB
                                                            Submit: 6974  Solved: 2661

Description

给出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

HINT

对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;

对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;

对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。

提示:

由于数据量很大,使用C++的同学请使用scanf和printf来进行输入输出操作,以免浪费不必要的时间。

 

解析:
       可入选BZOJ十大水题了。。。

       我用unordered_map和Treap都写了一遍,最后前者比后者快一倍,哈希就偷懒不写了。。。

 

代码(unordered_map):

#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;

const int Max=50005;
int t,n,x;
unordered_map<int,bool>vis;

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}
inline void print(int x)
{
	if(x<0) x=-x,putchar('-');
	if(x>9) print(x/10);
	putchar('0'+x%10);
}

int main()
{
	t=get_int();
	while(t--)
	{
	  vis.clear();
	  n=get_int();
	  for(int i=1;i<=n;i++)
	  {
	  	x=get_int();
	    if(vis[x]) continue;
	    vis[x]=1,print(x),putchar(' ');
	  }
	  if(t) putchar('\n');
	}
	return 0;
}

代码(Treap):

#include <bits/stdc++.h>
using namespace std;

const int Max=500005;
int t,n,m,root,tot,x,ans[Max];
struct shu{int l,r,num,data;};
shu a[Max];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}
inline void print(int x)
{
	if(x<0) x=-x,putchar('-');
	if(x>9) print(x/10);
	putchar('0'+x%10);
}
inline void clean()
{
	root=tot=m=0;
	memset(a,0,sizeof(a));
}

inline int NEW(int x){a[++tot].num=x,a[tot].data=rand();return tot;}
inline void zig(int &p)
{
	int q=a[p].l;
	a[p].l=a[q].r,a[q].r=p,p=q;
}
inline void zag(int &p)
{
	int q=a[p].r;
	a[p].r=a[q].l,a[q].l=p,p=q;
}

inline bool find(int p,int x)
{
	if(!p) return 0;
	if(a[p].num==x) return 1;
	if(x<a[p].num) return find(a[p].l,x);
	return find(a[p].r,x);
}

inline void Insert(int &p,int x)
{
	if(!p){p=NEW(x);return;}
	if(x<a[p].num)
	{
	  Insert(a[p].l,x);
	  if(a[a[p].l].data>a[p].data) zig(p);
	}
	else
	{
	  Insert(a[p].r,x);
	  if(a[a[p].r].data>a[p].data) zag(p);
	}
}

int main()
{
	t=get_int();
	while(t--)
	{
	  clean();
	  n=get_int();
	  for(int i=1;i<=n;i++)
	  {
	  	x=get_int();
	  	if(!find(root,x)) Insert(root,x),ans[++m]=x;
	  }
	  for(int i=1;i<=m;i++)
	  {
	  	print(ans[i]);
	  	if(i!=m) putchar(' ');
	  }
	  if(t) putchar('\n');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38083668/article/details/82940563
今日推荐