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集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...
随机推荐
- Laya 项目解耦
Manager解耦业务逻辑 Data解耦数据逻辑 View-UI解耦页面逻辑 ModuleController解耦通信逻辑
- 【Ts 4】ftp服务器搭建
一.为什么需要ftp? 分布式环境一般都有一个专门的图片服务器存放图片.我们使用虚拟机搭建一个专门的服务器来存放图片.在此服务器上安装一个nginx来提供http服务,安装一个ftp服务器来提供图片上 ...
- Java多线程干货系列—(四)volatile关键字
原文地址:http://tengj.top/2016/05/06/threadvolatile4/ <h1 id="前言"><a href="#前言&q ...
- 2016 Multi-University Training Contest 5 solutions BY ZSTU
ATM Mechine E(i,j):存款的范围是[0,i],还可以被警告j次的期望值. E(i,j) = \(max_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\ ...
- 【BZOJ2982】combination(Lucas定理)
题意:求C(n,m) n,m<=200000000 思路:c(n,m)=c(n mod mo,m mod mo)*c(n div mo,m div mo) mod mo (n>=mo或m& ...
- 使用imageMagick 制作圆角矩形和图片加水印
制作圆角矩形好图片水印都是图片合成的操作 composite -gravity southeast mask175.png src.jpg dest.jpg -gravity southeast ...
- mysql 时间类型datetime与timestamp区别比较
mysql 时间类型datetime与timestamp区别比较 相同点: 显示宽度和格式相同,显示宽度固定在19字符,格式为YYYY-MM-DD HH:MM:SS. 不同点: (1)时间范围不同: ...
- Swift--字典的了解
字典存储时,key和value值的类型都是固定的,且都是无序的. 1.字典类型的缩写语法 在swift中,字典的完整格式如下: Dictionary<Key, Value> 注意:字典的k ...
- 30分钟学会如何使用Shiro(转)
本文转自http://www.cnblogs.com/learnhow/p/5694876.html 感谢作者 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jin ...
- burpsuite破解版
来源:http://www.vuln.cn/8847