【bzoj2761】【JLOI2011】【不反复数字】【平衡树】
2024-09-30 08:39:44
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
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
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来进行输入输出操作,以免浪费不必要的时间。
题解:裸平衡树。
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int n,t,x;
bool f;
struct Node{
Node*ch[2];
int r,s,v;
Node(int v):v(v){ch[0]=ch[1]=NULL;r=rand();}
int cmp(int x){
if (x==v) return -1;
else return (x<v? 0:1);
}
};
Node* root;
void rotata(Node* &o,int d)
{
Node*k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;o=k;
}
void insert(Node* &o,int x)
{
if (o==NULL) o=new Node(x);
else
{
int d=o->cmp(x);
if (d==-1) {f=false;return;}
if (d!=-1)
{
insert(o->ch[d],x);
if (o->ch[d]->r>o->r) rotata(o,d^1);
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
root=NULL;
scanf("%d%d",&n,&x);f=true;
insert(root,x);
if (f) printf("%d",x);
for (int i=1;i<n;i++)
{
f=true;scanf("%d",&x);
insert(root,x);
if (f) printf(" %d",x);
}
printf("\n");
}
}
最新文章
- storage disk
- 关于HTML5、Jquery、Phonegap跨域问题的研究
- aes rsa加密
- JDK和环境配置
- Servlet小示例:jsp页面提交信息Servlet接收并打印输出
- strtotime 的几点不同
- [PWA] 8.Unobtrusive update: Delete old cache and only keep one, hard refresh to let new SW to take control
- 构建一个基于 Spring 的 RESTful Web Service
- 做web项目时对代码修改后浏览器端不生效的应对方法(持续更新)
- Hibernate3 第二天
- JS复习:第八章
- ReactiveCocoa常用方法
- iOS 搜索记录
- Java中Lambda表达式的使用(转)
- 1192:放苹果(dp + 搜索)
- HDFS高级功能
- 今日Q群:QQ群众群友反馈问题的归纳总结
- eclipse中改变默认的workspace的方法
- ABP学习入门系列(三) (领域层中的仓储Repository)
- node升级7.0以上版本使用gulp时报错
热门文章
- TOJ 2446: Mint
- 【JavaScript 13—应用总结】:锁屏遮罩
- POJ 2699 The Maximum Number of Strong Kings ——网络流
- BZOJ 1191: [HNOI2006]超级英雄Hero【二分图匹配】
- hdu4336 Card Collector(概率DP,状态压缩)
- JSP表单提交中文乱码
- 巴蜀2904 MMT数
- R语言入门视频笔记--6--R函数之cat、format、switch函数
- android控件-images
- 51 NOD 1406 and query