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. AJAX(XMLHttpRequest)进行跨域请求方法详解

    AJAX(XMLHttpRequest)进行跨域请求方法详解(三) 2010年01月11日 08:48:00 阅读数:24213 注意:以下代码请在Firefox 3.5.Chrome 3.0.Saf ...

  2. ROS Learning-032 (提高篇-010 Launch)Launch 深入研究 --- (启动文件编程)ROS 的 XML语法简介

    ROS 提高篇 之 Launch 深入研究 - 01 - 启动文件的编程 - ROS 的 XML语法简介 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubu ...

  3. 智能合约调用另一合约中的payable方法

    参考链接: https://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-anoth ...

  4. loadrunner怎样进行手动关联

    转自:http://novasblog.blog.163.com/blog/static/43713514200901341934981/ 手动关联的主要步骤为:        第一步:录制测试脚本, ...

  5. C++ STL 全排列函数

    C++  全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...

  6. Python爬虫实战五之模拟登录淘宝并获取所有订单

    经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...

  7. Spring.net页面属性注入

    .条件spring.web程序集 1.1 system.web配置 <httpHandlers> <add verb="*" path="*.aspx& ...

  8. C#变量初始化

    在C#中声明变量使用下述语法: datatype identifier;, 例如: int i; 该语句声明int变量i.编译器不允许在表达式中使用这个变量,除非用一个值初始化了改变量.如果你不需要使 ...

  9. [GO]匿名字段

    package main import ( "fmt" ) type Person struct { name string sex byte age int } type Stu ...

  10. a 标签 name 属性 页面定位 (二)

    <a href="to_url#somewhere">名字</a> <a name="somewhere">名字</a ...