给出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. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  2. 在mac下安装matplotlib,xlrd

    brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...

  3. captcha库报错"OSError: cannot open resource"

    问题描述 在win平台上python虚拟环境下使用captcha库生成验证码报错OSError: cannot open resource 代码 from captcha.image import I ...

  4. UltraEdit-14.10.0.1024版本语法着色配置

    用了UltraEdit有段时间了,一直没做语法着色,当做普通文本编辑器使用,这也太委屈这个“神器”了. 今天就让它物尽其用吧.体验一把UltraEdit的语法高亮功能. 参考:http://www.1 ...

  5. 同步OR异步?WebFlux开发真的比Servlet开发要快?顺便再科普下CompletableFuture

    在看下文之前,先给大家科普一点基础知识 Runable:线程任务类接口,没有返回值 Callable:与上面的不同就是有返回值 Executor:定义了线程池执行任务的接口,不过只定义了Runable ...

  6. python学习之 - XML

    xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...

  7. Python高级进阶(一)Python框架之Django入门

    传说中的Django Django由来 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的框架模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下 ...

  8. [Bzoj4540][Hnoi2016] 序列(莫队 + ST表 + 单调队列)

    4540: [Hnoi2016]序列 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1567  Solved: 718[Submit][Status] ...

  9. springboot 第一个程序

    idea --> new project --> 选择Spirng Initializr --> next 傻瓜式操作  --> 添加web依赖 项目基本结构: 创建contr ...

  10. weblogic负载分发

    博客分类: weblogic 负载均衡的实现方式有很多种,这里只介绍三种相对来说成本较低的方案(维护成本以及费用成本)weblogic自带的proxy.apache.nginx 1.weblogic自 ...