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 ...
随机推荐
- ROS(二)Service通信
使用自定义的消息类型,实现service方式的节点间双向通信 在package目录下创建msg和srv目录,存放package需要使用的.msg和.srv文件. 在ROS中,message被设计为一种 ...
- 本地生成Rails API文档
#新建一rails项目 rails new dummy cd dummy # 生成文档 rake doc:rails 剩下的就是等待了. 生成的文档在dummp/doc/api .浏览器打开index ...
- 【费元星】crt 无法上传文件,总是显示盾牌表示-完美解决
将如下内容保存到文件中,已.bat 结尾 taskkill /f /im explorer.exeattrib -s -r -h "%userprofile%\AppData\Local\i ...
- Linux怎样创建FTP服务器--修改用户默认目录-完美解决 - 费元星
在创建FTP服务器之有先命令: ps -ef |grep vsftpd 查一下系统有没有安装vsftpd这个服务器,如果出现如下图所示的界面说明没有安装. 然后再执行:yum install vs ...
- sql中的几种连接类型
一.连接类型简介 在sql中单表查询的几率相对来说比较少,随着数据库的日益复杂,多表关联的情况越来越多,在多表关联的情况下存在多种关联的类型, 1.自关联(join或inner join) 2.左外关 ...
- CentOS 7.2安装11g Grid Infrastructure
Preface Oracle claimed that 11g RAC is supported on Redhat Linux 7 and above version,but the ...
- Shell 常用命令、基本用法总结
Filter Filter 常用于从大量文本.数据中提取需求的部分.下面介绍几个常用的 filter 命令. cut $ cut -c 5-8 textfile.txt # 切出 textfile.t ...
- Dev c++ 调试步骤
不能调试的时候,修改下列地方: 1.在“工具”->编译选项->”Add following commands when calling complier”下面的编辑框里写入:-g3 2.在 ...
- The Activation Function in Deep Learning 浅谈深度学习中的激活函数
原文地址:http://www.cnblogs.com/rgvb178/p/6055213.html 版权声明:本文为博主原创文章,未经博主允许不得转载. 激活函数的作用 首先,激活函数不是真的要去激 ...
- ServiceStack.Ormlit 事务
应该使用这个方法开启事务 public static IDbTransaction OpenTransaction(this IDbConnection dbConn) { return new Or ...