923c C. Perfect Security
Trie树。
要求字典序最小,所以由前到后贪心的选择。建一个trie树维护b数列。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 300000*(20+5) ;
const int maxm = 30 + 10; int a[maxn],s[maxn],p0[maxn],p1[maxn],cnt;
int n,b; int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void build() {
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1,rhs,t;i<=n;i++) {
rhs=read();
t=0;
for(int j=29;j>=0;j--)
{
b=(rhs>>j&1);
if(b==0)
{
if(!p0[t]) p0[t]=++cnt;
t=p0[t];
}
else
{
if(!p1[t]) p1[t]=++cnt;
t=p1[t];
}
s[t]++;
}
} for(int i=1,k,t;i<=n;i++)
{
k=t=0;
for(int j=29;j>=0;j--)
{
b=(a[i]>>j)&1;
if(b==0)
if(p0[t]&&s[p0[t]])
{
t=p0[t];
k*=2;
//printf("0");
}
else
{
t=p1[t];
k=k*2+1;
//printf("1");
}
else
{
if(p1[t]&&s[p1[t]])
{
t=p1[t];
k=k*2+1;
}
else
{
t=p0[t];
k*=2;
}
}
s[t]--;
}
printf("%d ",a[i]^k);
}
printf("\n");
} void solve()
{ } int main()
{
build();
solve(); return 0;
}
923c C. Perfect Security的更多相关文章
- 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 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- 【CodeForces】947 C. Perfect Security 异或Trie
[题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...
- 01Trie树 CF923C Perfect Security
CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...
- Codeforces 948D Perfect Security
Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...
- Codeforces 948D Perfect Security 【01字典树】
<题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...
- 2018.12.08 codeforces 948D. Perfect Security(01trie)
传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...
- Codeforces 923 C. Perfect Security
http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...
- [CodeForces948D]Perfect Security(01字典树)
Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...
随机推荐
- java 编码二进制写法、十六进制用源代码表示
二进制: int a = 0b10; a其实=2 八进制: int a = 01; a其实=8 十六进制: int a = 0x1; a其实=16
- ROS Twist和Odometry消息类型使用(Python)
消息类型: 1. Twist - 线速度角速度 通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动 geometry_msgs/Twist geometry ...
- libevent学习七(bufferevent)
1. 每个bufferevent 都拥有类型为struct evbuffer的input buffer和out buffer,分别供数据读取和数据写入使用. 2.读取和写入数据是通过编写和设置对应的回 ...
- android分析windowManager、window、viewGroup之间关系(一)
本文将主要介绍addview方法,在windowManager.window.viewGroup中的实现原理.首先将介绍这些类结构关系,然后分析其内在联系,介绍实现原理,最后介绍重要的一个参数wind ...
- JVM--Java类加载机制
一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其存放在运行时数据区的方法区内,然后在java堆区创建一个java.lang.Class对象,用来封装类在方法区内 ...
- 关于java使用double还是float
眼睛一亮在论坛上发现一枚很有价值的评论赶紧抄下来... 记住java一定要用double,更鼓不变,就算数值不大也要用double.了解java虚拟机的底层会知道,float放在内存中其实是当作dou ...
- Skype for Business Server 方案
方案说明: 高可用性的配置屏蔽了单点故障,使得当一个服务器节点失效时,另外的可用的节点能够进行服务的接管.可伸缩性的配置可以保证当即时沟通平台的使用用户增加时,该平台应该具有良好的可伸缩性,能非常方便 ...
- 重装win10后ubuntu引导项修复
问题描述:原来是在win7下装了ubuntu14的双系统,后台win7换win10,然后使用EasyBCD进行引导项修复时,不好使,报 error file: /boot/grub/i386-pc/n ...
- POJ 3498 March of the Penguins(网络最大流)
Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...
- C语言--链表基础模板
1.建立结构体 struct ST { int num;///学号 int score;///成绩 struct ST*next; };///结构体 2.空链表的创建 struct ST creatN ...