题目链接: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(字典树)的更多相关文章

  1. Codeforces 948D Perfect Security 【01字典树】

    <题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...

  2. Codeforces 948D Perfect Security

    Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...

  3. 2018.12.08 codeforces 948D. Perfect Security(01trie)

    传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...

  4. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  5. Choosing The Commander CodeForces - 817E (01字典树+思维)

    As you might remember from the previous round, Vova is currently playing a strategic game known as R ...

  6. CodeForces 923C Perfect Security

    C. Perfect Security time limit per test3.5 seconds memory limit per test512 megabytes inputstandard ...

  7. Codeforces 282E Sausage Maximization(字典树)

    题目链接:282E Sausage Maximization 题目大意:给定一个序列A.要求从中选取一个前缀,一个后缀,能够为空,当时不能重叠.亦或和最大. 解题思路:预处理出前缀后缀亦或和,然后在字 ...

  8. Codeforces 271D - Good Substrings [字典树]

    传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  9. [CodeForces948D]Perfect Security(01字典树)

    Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...

随机推荐

  1. Python网络爬虫笔记(四):使用selenium获取动态加载的内容

    (一)  说明 上一篇只能下载一页的数据,第2.3.4....100页的数据没法获取,在上一篇的基础上修改了下,使用selenium去获取所有页的href属性值. 使用selenium去模拟浏览器有点 ...

  2. LeetCode题解之 Convert Sorted Array to Binary Search Tree

    1.题目描述 2.问题分析 使用二分法即可. 3.代码 TreeNode* sortedArrayToBST(vector<int>& nums) { ) return NULL; ...

  3. 超经典sql练习题,在teradata上实现

    题目来源:https://blog.csdn.net/flycat296/article/details/63681089 teradata实现: drop table student; create ...

  4. MySQL大数据表水平分区优化的详细步骤

    将运行中的大表修改为分区表 本文章代码仅限于以数据时间按月水平分区,其他需求可自行修改代码实现 1. 创建一张分区表 这张表的表字段和原表的字段一摸一样,附带分区 1 2 3 4 5 6 7 8 9 ...

  5. 实现一个协程版mysql连接池

    实现一个协程版的mysql连接池,该连接池支持自动创建最小连接数,自动检测mysql健康:基于swoole的chanel. 最近事情忙,心态也有点不积极.技术倒是没有落下,只是越来越不想写博客了.想到 ...

  6. sysbench压力工具报错:

    [root@ sysbench-]# /usr/local/sysbench/bin/sysbench --version : cannot open shared object file: No s ...

  7. docker18.ce harbor 安装

     Harbor 是什么? harbor VMware 开发的一个容器镜像仓库,harbor的功能提供用户权限管理.镜像复制等功能,提高使用的registry的效率. 安装最新版的docker可以参考d ...

  8. 不能用c99的情况下,如何动态定义数组的长度

    #include <stdio.h>#include <stdlib.h> int main(int argc, char const *argv[]){    int num ...

  9. Javascript之DOM性能优化

    原文地址:http://ce.sysu.edu.cn/hope/Item/140355.aspx 作者:陈古松 来源:本站原创 发布时间:2015-03-14 更新时间:2015-03-14  点击数 ...

  10. Spring 加载Controller逻辑的源码笔记

    org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#initHandlerMethods 进行加载Controll ...