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 ...
随机推荐
- 《SQL Server 2008从入门到精通》--20180716
1.锁 当多个用户同时对同一个数据进行修改时会产生并发问题,使用事务就可以解决这个问题.但是为了防止其他用户修改另一个还没完成的事务中的数据,就需要在事务中用到锁. SQL Server 2008提供 ...
- Huawei DHCP 中继配置实例
配置DHCP中继示例 组网需求 如图1,DHCP客户端所在的网段为10.100.0.0/16,而DHCP服务器所在的网段为202.40.0.0/16.需要通过带DHCP中继功能的设备中继DHCP报文, ...
- How to add hyperlink in JLabel
You can do this using a JLabel, but an alternative would be to style a JButton. That way, you don't ...
- CentOS7 中安装 MySQL
0. 说明 参考 centos7.2安装MySQL CentOS 7 下 Yum 安装 MySQL 5.7 两种方式安装 MySQL 安装 MySQL(yum) & 安装 MySQL(yum) ...
- Android5.0中Material Design的新特性
最近项目中需要用到Material Design,整理了下面几个常用的控件,以便记忆. 一.Snackbar 1.作用:与Toast类似,但是可以点击监听: 2.使用: (1)Snackbar调用静态 ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 2018.08.31 16:26 调试 Swift 和 Pycharm 与 github 之间的链接
花了一段时间调试Swift和Pycharm的链接,网上查了一下有关信息,再加上自己的摸索,一会就掌握了.
- leetcode 1. Two Sum [java]
注意点: HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null; public int[] tw ...
- Sqlite的安装和简单使用
Sqlite 1 安装 首先,下载相应的版本: https://sqlite.org/download.html 其次,解压到本地,并添加到环境变量. 然后,打开 CMD 创建,输入 sqlite3 ...
- Grafana3.0.1+Zabbix3.0.4监控系统平台搭建
前言 本文的Zabbix部分知识只介绍它的基础安装,Zabbix的使用以及配置优化并不在本文的介绍范围之内. 本文只介绍在CentOS6系列下的安装和部署,其他发行版与其他版本号暂不涉及 本文默认使用 ...