Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security
题意:给出N个数代表密码,再给出N个数代表key。现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小。
题解:要使密码组里面每个数都找到能使其亦或和最小的数可以将key建成一棵字典树(这里建树方式很关键,可以每个数都从2^31开始建树,这样可以使我们在遍历树查询更加方便)。之后再遍历密码组每次在字典树里面找到一个能使它亦或和最小的数,再将这个数从字典树中删掉。。。 字典树太久不写,很多东西都忘记了!
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5+;
const int INF = 1e9+;
int tran[MAX_N*][]; //跳转到下一个节点的数组
int sum[MAX_N*],ans[MAX_N],A[MAX_N],P[MAX_N]; //sum - 记录每个节点记录的数的数量
int N,M,T,S,siz;
void init(){
for(int i=;i<MAX_N;i++){
tran[i][] = tran[i][] = ;
sum[i] = ;
}
siz = ;
}
void _insert(int x){
int now = ;
for(int i=;i>=;i--){ //从31开始就可以保证从高位开始建树了
int t = ;
if(x & (<<i)) t = ;
if(!tran[now][t]) tran[now][t] = ++siz; // 这里siz指的时节点的编号,这里如果下一个指向的节点没有的话就新生成一个节点给它。
sum[tran[now][t]] ++; //节点上记录的数的数量加一
now = tran[now][t]; //跳转到下一个节点
}
}
void query(int id , int x){
int now = ;
int res = ;
for(int i=;i>=;i--){
int t =;
if(x & (<<i)) t = ;
if(!sum[tran[now][t]]){
res += (<<i);
t = -t;
}
sum[tran[now][t]] --;
now = tran[now][t]; }
ans[id] = res;
}
int main()
{
while(cin>>N)
{
init();
for(int i=;i<N;i++){
scanf("%d",&A[i]);
}
for(int i=;i<N;i++){
scanf("%d",&P[i]);
_insert(P[i]);
}
for(int i=;i<N;i++){
query(i,A[i]);
cout<<ans[i]<<" ";
}
cout<<endl; }
return ;
}
Codeforces 948D Perfect Security(字典树)的更多相关文章
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
- Codeforces 948D Perfect Security
Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...
- 2018.12.08 codeforces 948D. Perfect Security(01trie)
传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- CodeForces 923C Perfect Security
C. Perfect Security time limit per test3.5 seconds memory limit per test512 megabytes inputstandard ...
- Codeforces 282E Sausage Maximization(字典树)
题目链接:282E Sausage Maximization 题目大意:给定一个序列A.要求从中选取一个前缀,一个后缀,能够为空,当时不能重叠.亦或和最大. 解题思路:预处理出前缀后缀亦或和,然后在字 ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- [CodeForces948D]Perfect Security(01字典树)
Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...
随机推荐
- Problem5-Project Euler
Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to ...
- [Python_5] Python 线程
0. 说明 Python 线程笔记 1. 低级 API # -*-coding:utf-8-*- """ 线程 """ "&quo ...
- October 18th 2017 Week 42nd Wednesday
Only someone who is well-prepared has the opportunity to improvise. 只有准备充分的人才能够尽兴表演. From the first ...
- DevExpress07、DataNavigator、 ControlNavigator
https://documentation.devexpress.com/WindowsForms/DevExpress.XtraEditors.DataNavigator.class 1.DataN ...
- node学习笔记_03 express框架
express api地址:http://www.expressjs.com.cn/starter/static-files.html 一.安装依赖 npm i --save express npm ...
- python is、==区别;with;gil;python中tuple和list的区别;Python 中的迭代器、生成器、装饰器
1. is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同 == 比较的是两个对象的内容是否相等 2. with语句时用于对try except finally 的优 ...
- OpenCV——Harris、Shi Tomas、自定义、亚像素角点检测
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- centos7上svn安装
svn安装 yum install subversion 查看svn安装的版本 svnserve --version新建svn目录 mkdir /opt/svn 建立版本库目录 mkdir ...
- 编程检查d:\test.txt文件是否存在,若在则显示该文件的名称和内容。
下面放两种方法 老师写的: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- day31
今日内容 在python中使用TCP协议的客户端与服务端进行通信 服务端代码: ############################################################ ...