【CodeForces】947 C. Perfect Security 异或Trie
【题意】给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小。n<=3*10^5,time=3.5s。
【算法】异或Trie
【题解】对一个数组O(n log n)建立异或Trie,就能O(log n)判断任意一个数在这个数组中异或值最大的数。
所以对B建异或Trie(每个数字从高二进制位开始插入),然后数组A依次在Trie上跑,从上到下尽量跑向相同数字边,这样得到字典序最小,路径中顺便删除标记。
复杂度O(n log n)。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define ll long long
#define lowbit(x) x&-x
using namespace std;
int read(){
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a<b?b:a;}
int ab(int x){return x>?x:-x;}
//int MO(int x){return x>=MOD?x-MOD:x;}
//void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
/*------------------------------------------------------------*/
const int inf=0x3f3f3f3f,maxn=;
int ch[maxn*][],sz,num[maxn**],a[maxn]; int n;
void insert(int y){
int x=;
for(int i=;i>=;i--){
bool k=(y&(<<i))>;
if(!ch[x][k])ch[x][k]=++sz;
num[ch[x][k]]++;
x=ch[x][k];
}
}
int find(int y){
int x=;
for(int i=;i>=;i--){
bool k=(y&(<<i))>;
if(num[ch[x][k]]>){
x=ch[x][k];
num[x]--;
y^=((k)<<i);
}
else{
x=ch[x][!k];
num[x]--;
y^=((!k)<<i);
}
}
return y;
}
int main(){
n=read();
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<=n;i++)insert(read());
for(int i=;i<=n;i++)printf("%d ",find(a[i]));
return ;
}
【CodeForces】947 C. Perfect Security 异或Trie的更多相关文章
- Codeforces 923 C. Perfect Security
http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...
- CodeForces 923C Perfect Security
C. Perfect Security time limit per test3.5 seconds memory limit per test512 megabytes inputstandard ...
- Codeforces 948D Perfect Security
Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...
- Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- 01Trie树 CF923C Perfect Security
CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...
- [codeforces 317]A. Perfect Pair
[codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...
- Codeforces 980 D. Perfect Groups
\(>Codeforces\space980 D. Perfect Groups<\) 题目大意 : 设 \(F(S)\) 表示在集合\(S\)中把元素划分成若干组,使得每组内元素两两相乘 ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
随机推荐
- Java包名命名规则(转载)
转载自:http://lilinhai548.blog.163.com/blog/static/5847332920155132151359/ 鸣谢原作者 学习Java的童鞋们都知道,Java的包. ...
- 只要实现了annotation这个接口就是注解 同理:只要实现了某个接口就是该类型的实现类
只要实现了annotation这个接口就是注解 同理:只要实现了某个接口就是该类型的实现类
- AngularJS中$watch
$watch在digest执行时,如果watch观察的value与上一次执行时不一样时,就会被触发.angularjs内部的watch实现了页面随model的及时更新.$watch 方法在用的时候主要 ...
- BZOJ3637 Query on a tree VI(树链剖分+线段树)
考虑对于每一个点维护子树内与其连通的点的信息.为了换色需要,记录每个点黑白两种情况下子树内连通块的大小. 查询时,找到深度最浅的同色祖先即可,这可以比较简单的树剖+线段树乱搞一下(似乎就是qtree3 ...
- 洛谷 P4114 Qtree1
Qtree系列都跟树有着莫大的联系,这道题当然也不例外 我是题面 读完题,我们大概就知道了,这道题非常简单,可以说是模板题.树剖+线段树轻松解决 直接看代码吧 #include<algorith ...
- [BJWC2011]元素 线性基
题面 题面 题解 一个方案合法,当且仅当选取的01串凑不出0. 因此就是要使得选取的01串全在线性基内,具体原因可以看这道题:[CQOI2013]新Nim游戏 线性基 要使得魔力值最大,只需要按法力值 ...
- SSH不能连接并提示REMOTE HOST IDENTIFICATION HAS CHANGED解决
SSH不能连接并提示REMOTE HOST IDENTIFICATION HAS CHANGED解决方法: 如果提示信息如下: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- oracle-DECODE()函数
DECODE()函数 DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ) 含义解释: DECODE(条件,值1,翻译值1,值2,翻 ...
- JSP2 的自定义标签
在 JSP 中开发标签库只需如下几个步骤 1.开发自定义标签处理类 2.建立一个 *.tld 文件,每个 *.tld 文件对应一个标签库,每个标签库可包含多个标签 3.在 JSP 文件中使用自定义标签 ...
- thinkphp使用with对关联数据进行预加载
1.with('relation'),只预加载relation这个关联,如下面 public function relation() { return $this->hasOne(Relatio ...