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 ...
随机推荐
- C#多线程的用法6-线程间的协作Mutex
Mutex在线程协作的过程中起互斥的左右,效果与线程锁类似. /// <summary> /// 多线程协作-Mutex /// </summary> private stat ...
- SQL Server在本地计算机上用SSMS(SQL Server Management Studio)登录不上,错误消息:(Microsoft SQL Server, Error: 18456)
今天遇到了一个奇怪的问题,公司目前在SQL Server上都采用AD域账号登录,由于账号人数众多,所以我们建立了一个AD Group(域组),将大家的AD账号加入了这个AD Group,然后我们将这个 ...
- 详解JNDI的lookup资源引用java:/comp/env
ENC的概念: The application component environment is referred to as the ENC, the enterprise naming c ...
- [Spark Streaming_1] Spark Streaming 概述
0. 说明 Spark Streaming 介绍 && 在 IDEA 中编写 Spark Streaming 程序 1. Spark Streaming 介绍 Spark Stream ...
- windows10下安装source insight 4.0(破解版)
1.从官网下载source insight4.0版本(不用下载,在后面已经把所有需要的文件都准备好了); 2.安装source insightt4.0; 3.使用下载好的sourceinsight4. ...
- Docker 使用入门,创建一个Nginx服务器
运行环境: MAC Docker 版本: Docker version 17.12.0-ce, build c97c6d6 一.启动Nginx 服务器 启动Nginx 服务器,并进入模拟终端 dock ...
- MySQL报错:error1130
ERROR (HY000): Host 'ip-172-31-x-x.ec2.internal' is not allowed to connect to this MySQL server 分析,从 ...
- iOS 固定定位不兼容、input获取焦点后位置不对。
第一次写博客~ 大家悠着看,有则改之,无则加冕,对不对的给个回复,让我知道你的存在. 在做活动页的时候,经常会碰到一些需要弹出显示的输入框(情节前提:本人安卓机~),前天自己写的时候自己调试没问题后 ...
- UI(四)之拓扑图创建
关键函数: 1.LoadMap void CTopology::LoadMap() { //m_map.RemoveAllLayers(); AddLayersBasemap(); //AddLaye ...
- document.getElementByClassName的兼容问题
if(!document.getElementsByClassName){ document.getElementsByClassName = function(className, element) ...