Codeforces 948D Perfect Security 【01字典树】
<题目链接>
题目大意:
给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小。
解题分析:
用01字典树来解决异或最值问题。因为是改变第二个序列的顺序,即按照第一个序列的顺序输出异或结果,所以我们将第二个序列建树。然后用第一个序列在树上进行查询。然后就是01字典树基本操作,因为每个数能够被使用一次,所以每个节点加上$num[N]$数组标记,并且加上del操作。
#include <bits/stdc++.h>
using namespace std; const int N = 3e5+;
int n,pos=;
int nxt[N*][],val[N*],num[N*],A[N],P[N]; void Insert(int x){ //利用该数的二进制进行建树
int now=;
for(int i=;i>=;i--){
int to=(x>>i)&;
if(!nxt[now][to])nxt[now][to]=++pos;
now=nxt[now][to];
num[now]++;
}
val[now]=x;
}
void del(int x){ //从Trie树上删除使用过的数
int now=;
for(int i=;i>=;i--){
int to=(x>>i)&;
now=nxt[now][to];
num[now]--;
}
}
int query(int x){
int now=;
for(int i=;i>=;i--){
int to=(x>>i)&;
if(nxt[now][to] && num[nxt[now][to]])now=nxt[now][to]; //因为是使异或结果最小,所以尽可能与该数的二进制相同
else now=nxt[now][to^];
}
del(val[now]);
return x^val[now];
}
int main(){
scanf("%d",&n);
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++)
printf("%d ",query(A[i]));
}
2019-03-02
Codeforces 948D Perfect Security 【01字典树】的更多相关文章
- Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- Codeforces 948D Perfect Security
Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...
- 2018.12.08 codeforces 948D. Perfect Security(01trie)
传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...
- [CodeForces948D]Perfect Security(01字典树)
Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树
A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...
随机推荐
- SQL Server 数据恢复到指点时间点(完整恢复)
SQL Server 数据恢复到指点时间点(完整恢复) 高文龙关注2人评论944人阅读2017-03-20 12:57:12 SQL Server 数据恢复到指点时间点(完整恢复) 说到数据库恢复,其 ...
- 图片文字css小知识点
行内元素,图片和文字中间有缝隙,需要给父元素设置font-size:0: 图片和文字不对齐,给图片设置vertical-align:top 文字行高有缝隙 设置vertical-align:top
- iframe与主框架跨域相互访问方法
iframe 与主框架相互访问方法 http://blog.csdn.net/fdipzone/article/details/17619673/ 1.同域相互访问 假设A.html 与 b.htm ...
- 【CSS】Bootstrap中select2+popover冲突
网上搜索得到: It changes the position because the position is based on the popover's dimansions and select ...
- java Properties (属性集)
加载Properties Properties downloadLog = new Properties(); try { //加载logFile文件 downloadLog.load(new Fil ...
- tomcat启动报错:Annotation-specified bean name 'patrolTrailServiceImpl' for bean class [cn.oppo.inventoryService.patrolTrailServiceImpl] con
注释-为bean类[目录服务patrolTrailServiceImpl]指定的bean名称“patrolTrailServiceImpl”与同名和[目录服务patrolTrailServiceImp ...
- ajax--参数映射方式实现阴影效果
注:通过json对象的方式传递参数,参数具体信息由json对象来封装,参数封装到对象中再进行映射(参数映射) shadow.js //使用参数映射方式实现参数设置/* option:参数对象,封装所有 ...
- 为什么访问json接口出现文件下载
在IE9,10,11下,当服务器端返回数据格式为json,且明确设置Content-Type为”application/json;charset=utf-8“时,会提示文件下载.如图所示: 解决办法是 ...
- resources中添加配置文件
- asp.net core 中间件应用
中间件是一种装配到应用管道以处理请求和响应的软件. 每个组件: 选择是否将请求传递到管道中的下一个组件. 可在调用管道中的下一个组件前后执行工作. 请求委托(Request delegates)用于生 ...