【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字典树来解决异或最值问题.因为 ...
随机推荐
- ElasticSearch API 简要介绍
调用其API会返回很多信息,例如集群的信息,节点的信息等 检查集群的状态----Restful API说明 1:检查集群状态信息 2:管理集群 3:执行 增删改查 命令 4:执行高级命令 Restfu ...
- 软工实践团队展示——WorldElite
软工实践团队展示--WorldElite 本次作业链接 团队成员 031602636许舒玲(组长) 031602237吴杰婷 031602634吴志鸿 081600107傅滨 031602220雷博浩 ...
- laya3d 文件格式
先认识下laya3d的一些文件 导出文件说明: ls --- 场景文件: Json文件,包含场景中所有节点的数据信息,包含光照贴图信息 ...
- PHP中与类和对象有关的几个系统函数
与类有关的系统函数: class_exists(“类名”), 判断一个类是否存在(是否定义过) interface_exists(“接口名”), 判断一个接口是否存在(是否定义过) get_class ...
- bzoj2013[CEOI2010] A huge tower
题意 有N(2<=N<=620000)快砖,要搭一个N层的塔,要求:如果砖A恰好在砖B上面,那么A不能比B的长度+D要长.问有几种方法,输出 答案 mod 1000000009的值 分析 ...
- 【uoj#174】新年的破栈 贪心
题目描述 给你一个长度为 $n$ 的序列和一个空的双端队列,每次进行3种操作种的一种: 1.将序列中编号最小的数加入到双端队列的队尾:2.从双端队列的队尾取出一个数:3.从双端队列的队头取出一个数. ...
- 修改gcc/g++默认include路径
修改gcc/g++默认include路径 转自:http://www.network-theory.co.uk/docs/gccintro/gccintro_23.htmlhttp://ilewen. ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...
- BZOJ5315 [JSOI2018]防御网络 【仙人掌 + dp】
题目链接 BZOJ5315 题解 题目好吓人= =点仙人掌 + 斯坦纳树 我们只需求出对于所有选点的方案的斯坦纳树边长总和 \(n\)那么大当然不能状压,但是考虑一下如果这是一棵树,一个方案的贡献就是 ...
- 70路小报:用PV和UV作为网站衡量指标已经过时
方法]投资人呼吁:PV和UV不应该再作为产品衡量指标 风险投资机构Andreessen Horowitz近日一直反对再用传统的网站衡量指标去评价互联网产品,比如PV和UV,甚至包括应用的下载量. 他们 ...