给出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]=;
}
}
return ;
}

二叉树:764ms

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct Splay
{
int ch[maxn][],fa[maxn],key[maxn],rt,cnt;
void init(){
rt=cnt=;
}
int get(int x) { return ch[fa[x]][]==x; }
bool insert(int x)
{
int Now=rt,f=;
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]=;
ch[cnt][]=ch[cnt][]=;
return false;
}
key[++cnt]=x;
fa[cnt]=f; ch[f][key[f]<x]=cnt;
ch[cnt][]=ch[cnt][]=;
//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^]; fa[ch[x][opt^]]=old;
ch[x][opt^]=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 ;
}

splay:1208ms

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct Splay
{
int ch[maxn][],fa[maxn],key[maxn],rt,cnt;
void init(){
rt=cnt=;
}
int get(int x) { return ch[fa[x]][]==x; }
bool insert(int x)
{
int Now=rt,f=;
while(Now){
if(key[Now]==x){
splay(Now,);
return true;
}
f=Now;
Now=ch[Now][key[Now]<x];
}
if(!rt){
rt=++cnt; key[cnt]=x; fa[cnt]=;
ch[cnt][]=ch[cnt][]=;
return false;
}
key[++cnt]=x;
fa[cnt]=f; ch[f][key[f]<x]=cnt;
ch[cnt][]=ch[cnt][]=;
splay(cnt,);
return false;
}
void rotate(int x)
{
int old=fa[x],fold=fa[old],opt=get(x);
ch[old][opt]=ch[x][opt^]; fa[ch[x][opt^]]=old;
ch[x][opt^]=old; fa[old]=x; fa[x]=fold;
if(fold) ch[fold][ch[fold][]==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 ;
}

BZOJ2761:不重复数字(splay效率对比)的更多相关文章

  1. [BZOJ2761][JLOI2011]不重复数字

    [BZOJ2761][JLOI2011]不重复数字 试题描述 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复 ...

  2. BZOJ2761 不重复的数字 【treap】

    2761: [JLOI2011]不重复数字 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 5517  Solved: 2087 [Submit][S ...

  3. [BZOJ2761] [JLOI2011] 不重复数字 (C++ STL - set)

    不重复数字 题目:         给出N个数,要求把其中重复的去掉,只保留第一次出现的数.例如,给出的数 为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 1 ...

  4. c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习

    c#中@标志的作用   参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/toke ...

  5. JAVA实现随机无重复数字功能

    本文给大家介绍如何在JAVA中实现随机无重复数字的功能.如果您是初学者的话,有必要看一看这篇文章,因为这个功能一般会在面试中遇到.包括我本人在招聘人员的时候也喜欢拿这个问题去问别人,主要看一看考虑问题 ...

  6. C/C++面试之算法系列--去除数组中的重复数字

    去除数组中的重复数字 Sailor_forever  sailing_9806@163.com 转载请注明 http://blog.csdn.net/sailor_8318/archive/2008/ ...

  7. Snapman系统中TCC执行效率和C#执行效率对比

    Snapman集合了TCC编译器可以直接编译执行C语言脚本,其脚本执行效率和C#编译程序进行效率对比,包括下面4方面: 1.函数执行效率 2.数字转换成字符串 3.字符串的叠加 4.MD5算法 这是C ...

  8. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  9. List集合去重方式及效率对比

    List集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...

随机推荐

  1. 使用SqlParameter.SqlDbType和SqlParameter.Size时需要注意的地方

    1.DbParameter类是SqlParameter和OracleParameter类的父类.DbParameter.Size用来获取或设置列中数据的最大尺寸(只对文本数据有用). 2.数据类型Ch ...

  2. .NET下 JSON 的一些常用操作

    1.JSON的序列化和反序列化 Newtonsoft.Json dll 下载地址http://json.codeplex.com/ using System; using System.Collect ...

  3. HDU 3062 简单的2-SAT问题

    在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){    int m=2*i+a;    int n=2*j+b;  ...

  4. Eclipse安装插件长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示" ...

  5. 如何在Eclipse中生成Native类对应的JNI的.h文件

    1 致谢 感谢super_level网友 他的博客写的很清楚 给了我很多帮助 链接如下:http://blog.csdn.net/super_level/article/details/2124353 ...

  6. navicat 无法连接到腾讯云Mysql

    远程连接进入服务器 远程连接进入服务器之后,输入命令mysql -u root -p 之后输入mysql密码,进入mysql 命令环境 设置开启远程登录 GRANT ALL PRIVILEGES ON ...

  7. Asp.net core使用MediatR进程内发布/订阅

    1.背景 最近,一个工作了一个月的同事离职了,所做的东西怼了过来.一看代码,惨不忍睹,一个方法六七百行,啥也不说了吧,实在没法儿说.介绍下业务场景吧,一个公共操作A,业务中各个地方都会做A操作,正常人 ...

  8. leetcode笔记:Longest Substring Without Repeating Characters

    一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...

  9. [转]JS 引擎的执行机制

    转: https://www.cnblogs.com/wancheng7/p/8321418.html ------------------------------------------------ ...

  10. easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式

    easyUI 验证控件应用.自己定义.扩展验证 手机号码或电话话码格式 在API中   发现给的demo 中没有这个验证,所以就研究了下. 相关介绍省略,直接上代码吧! watermark/2/tex ...