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集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...
随机推荐
- asp.net静态变量研究
asp.net的webform,请求一个页面,如index.aspx,每一次都会交给不同的线程来处理. 经过个人测试,不管是页面类的静态属性,还是工具类的静态属性,都不会因为session的过期而改变 ...
- PHP 基础复习 2018-06-17
(1)AJAX Asynchronous JavaScript And XML(异步 JavaScript 及 XML) (2)创建 XMLHttpRequest 对象 不同的浏览器使用不同的方法来创 ...
- Java使用IText(VM模版)导出PDF
Java使用IText(VM模版)导出PDF: public String createPDF(ProjectManageBase projectManageBase) { Map map = new ...
- solr相关文章
Solr集群架构概述及delta-import详细配置 背景 由于项目原因,重新熟悉了下Solr,版本为3.6,搭建了主从Solr服务,并使用DIH从RDBMS数据源增量更新索引. 其实也没什么技术含 ...
- django学习之- Form
参考:http://www.cnblogs.com/wupeiqi/articles/6144178.htmlFORM中的字段只对post上来的数据进行form验证,主要涉及:字段 和 插件字段:对用 ...
- D. Little Artem and Dance---cf669D
http://codeforces.com/problemset/problem/669/D 题目大意: 有n对人 男生和女生 开始时 每个人的标号是(1,2,3,...n) 女生们围成一个圈 男 ...
- 109.Convert sorted list to BST
/* * 109.Convert sorted list to BST * 2016.12.24 by Mingyang * 这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的. * 这时候 ...
- Eclipse的JQuery提示插件-Spket(别试了,没什么效果,且安装设置麻烦)
参考: http://www.cnblogs.com/shulin/archive/2010/08/09/1796146.html 我测试了,但是没用起来,原因有如下几点: 1.配置复杂,且提示效果不 ...
- WinCE5.0如何安装.NET3.5
首先去微软官网下载.NET Compact Framework 3.5 Redistributable 点击下载 下载页面 一共有两种安装方式,我们先介绍常规的安装方式 1.设备连接到电脑,然后双击下 ...
- topcoder srm 552
div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...