BZOJ2761:不重复数字(splay效率对比)
Input
Output
Sample Input
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效率对比)的更多相关文章
- [BZOJ2761][JLOI2011]不重复数字
[BZOJ2761][JLOI2011]不重复数字 试题描述 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复 ...
- BZOJ2761 不重复的数字 【treap】
2761: [JLOI2011]不重复数字 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 5517 Solved: 2087 [Submit][S ...
- [BZOJ2761] [JLOI2011] 不重复数字 (C++ STL - set)
不重复数字 题目: 给出N个数,要求把其中重复的去掉,只保留第一次出现的数.例如,给出的数 为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 1 ...
- 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 ...
- JAVA实现随机无重复数字功能
本文给大家介绍如何在JAVA中实现随机无重复数字的功能.如果您是初学者的话,有必要看一看这篇文章,因为这个功能一般会在面试中遇到.包括我本人在招聘人员的时候也喜欢拿这个问题去问别人,主要看一看考虑问题 ...
- C/C++面试之算法系列--去除数组中的重复数字
去除数组中的重复数字 Sailor_forever sailing_9806@163.com 转载请注明 http://blog.csdn.net/sailor_8318/archive/2008/ ...
- Snapman系统中TCC执行效率和C#执行效率对比
Snapman集合了TCC编译器可以直接编译执行C语言脚本,其脚本执行效率和C#编译程序进行效率对比,包括下面4方面: 1.函数执行效率 2.数字转换成字符串 3.字符串的叠加 4.MD5算法 这是C ...
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- List集合去重方式及效率对比
List集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...
随机推荐
- Flask--登录检查(简单篇)---xunfeng巡风实例篇
如果session返回的是loginsuccess,那么通过redirect直接跳转到执行的页面或者view. 同时通过装饰器的形式.. # -*- coding: UTF-8 -*- from fu ...
- Go切片的操作
package main import "fmt" //切片的操作 func main() { //创建slice var s []int //zero value for sli ...
- [转]Android SDK下载和更新失败的解决方法
今天更新sdk,遇到了更新下载失败问题: Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xmlFetched ...
- 栈 练习 Codevs 3137 3138 3139
3137 栈练习1 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只 ...
- msp430项目编程01
msp430中项目---点阵LED显示 1.点阵LED介绍 2.代码(直接使用引脚驱动) 3.代码(使用芯片驱动) 4.项目总结 msp430项目编程 msp430入门学习
- BZOJ——2563: 阿狸和桃子的游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=2563 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: ...
- 洛谷 P3879 [TJOI2010]阅读理解
P3879 [TJOI2010]阅读理解 题目描述 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. 输入输出 ...
- 微服务 Framework
Dubbo :https://github.com/dubbo Spring Cloud :https://github.com/spring-cloud
- kindle】扫描版PDF完美切割六寸
kindle]扫描版PDF完美切割六寸 半夏 2013-11-05 18:36:01 软件来源记不清了..连使用说明的网址都找不到了,自己写一下使用方法大家凑合一下呗~ 软件是大牛写的 ...
- linux nginx service nginx restart [fail]
命令:nginx -t 查看失败原因: nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/sit ...