CF923C Perfect Security

上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果

01Trie树。

记录每个节点经过多少次。

每一次查询的时候将经过的点的标记加1.

那么当我们要去走某一个节点的时候,就去判断他的标记是否小于初始的值,如果不是那么就不能走。

教训告诉我们一定要想好细节再打代码,我没想好怎么处理初始标记就去写了个拓扑排序。低智低智。。。

code:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring> using namespace std; const int wx=300017; inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
} queue<int > q;
int a[wx];
int p[wx];
int n; struct Trie{
int tr[wx*33][2];
int cnt;
int son[wx*33];
int tot[wx*33]; void clear(){
memset(son,0,sizeof son);
memset(tot,0,sizeof tot);
} void insert(int x){
int p=0;
for(int i=33;i>=0;i--){
int k=((x&(1ll<<i))?1:0);
if(!tr[p][k])tr[p][k]=++cnt;
p=tr[p][k]; son[p]++;
}
q.push(p);
} int query(int x){
int p=0; int re=0;
for(int i=33;i>=0;i--){
int k=((x&(1ll<<i))?1:0);
if(tr[p][k]&&tot[tr[p][k]]<son[tr[p][k]])
p=tr[p][k];
else p=tr[p][k^1],re+=(1ll<<i);
tot[p]++;
}
return re;
} }Trie; int main(){
n=read(); Trie.clear();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++)p[i]=read(),Trie.insert(p[i]);
for(int i=1;i<=n;i++)printf("%d ",Trie.query(a[i]));
puts("");
return 0;
}

01Trie树 CF923C Perfect Security的更多相关文章

  1. Codeforces 948D Perfect Security(字典树)

    题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...

  2. Codeforces 948D Perfect Security

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

  3. bzoj 4137 [FJOI2015]火星商店问题——线段树分治+可持久化01trie树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4137 关于可持久化01trie树:https://www.cnblogs.com/LadyL ...

  4. 【CodeForces】947 C. Perfect Security 异或Trie

    [题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...

  5. 可持久化01Trie树【p4735(bzoj3261)】最大异或和

    Description 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的长度\(N ...

  6. CodeForces 923C Perfect Security

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

  7. 可持久化0-1Trie树

    我跟可持久化数据结构杠上了 \(QwQ\) .三天模拟赛考了两次可持久化数据结构(主席树.可持久化0-1Trie树),woc. 目录: 个人理解 时空复杂度分析 例题及简析 一.个人理解 可持久化0- ...

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

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

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

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

随机推荐

  1. Maven01 环境准备、maven项目结构、编译/测试/打包/清除、安装、

    0 前提准备 0.1 安装java开发环境 0.2 安装maven工具 1 maven项目基本结构 如图所示,整个maven项目有业务文件.测试文件.POM依赖管理文件:其实还有一个资源文件resou ...

  2. 待解决:2bootstrap-cerulean.css Failed to load resource: the server responded with a status of 404 ()

    2bootstrap-cerulean.css Failed to load resource: the server responded with a status of 404 ()

  3. Opencv轮廓计数(学习)

    #include <iostream>#include <opencv2/opencv.hpp>#include <opencv2/xfeatures2d.hpp> ...

  4. spring+springmvc+mybatis+redis实现缓存

    先搭建好redis环境 需要的jar如下: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:330 ...

  5. js setTimeout 和 setInterval 区别

    setTimeout和setInterval的语法相同.它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码.不过这两个函数还是有区别的 ...

  6. lintcode-单例

    单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 ...

  7. Insufficient free space for journal files

    前两天请假了,公司的很多app突然挂掉了,说是mongodb莫名的挂掉了,赶紧进去看了看日志: --31T14:: [initandlisten] ERROR: Insufficient free s ...

  8. Storm中并行度原来是这样计算的(1.0.1版本)

    ==思考问题1== 向集群提交一个拓扑的时候,Storm是如何计算Task数以及Executor数的? 具体有多少个worker,多少个executor,每个executor负责多少个task? == ...

  9. 使用DBMS_SCHEDULER包管理计划任务

    Dbms_scheduler是Oracle提供创建计划任务的包,任务类型可以是执行PL\SQL程序.执行外部脚本.调用操作系统命令,通常用于创建定期定时的任务,不依赖操作系统,保存在数据库内,数据库迁 ...

  10. Notepad++ xml/json格式化

    Xml格式化: 1. 安装XML Tools插件 (1) 通过网址http://sourceforge.net/projects/npp-plugins/files/XML%20Tools/下载XML ...