题意:
给定两个长为n的数组a和b;
重新排列a和b,生成数组c,c[i]=a[i] xor b[i];
输出字典序最小的c数组。

分析:
将a中的数插入一颗01字典树a中;
将b中的数插入一颗01字典树b中;
在trie树上查找n次,每次同时在a和b中下移一层;
if 能同时走0,则同时走0;
else if 能同时走1,则同时走1;
else if 树a能走0&&树b能走1,则a走0、b走1;
else if 树a能走1&&树b能走0,则a走1、b走0;
else 向c中插入一个新数为这两个节点的异或值;
最后对c排序。
复杂度O(T*n*30)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+;
typedef long long LL;
struct Trie01{ int ch[ * maxn][];
int cnt[*maxn];
int node_cnt;
inline void init(){
node_cnt = ;
memset(ch[],,sizeof(ch[])); }
inline void Insert(LL x){
int cur = ;
for(int i = ;i >= ;--i){
int idx = (x >> i) & ;
if(!ch[cur][idx]){
memset(ch[node_cnt],,sizeof(ch[node_cnt]));
ch[cur][idx] = node_cnt;
cnt[node_cnt++]=;
}
cur = ch[cur][idx];
cnt[cur]++;
}
}
}t1,t2;
vector<int> ans;
void solve(int N){
int u1,u2;
while(N--) {
u1 = u2 = ;
int x = ;
for(int p = ; p >= ; p--) { if(t1.cnt[t1.ch[u1][]] && t2.cnt[t2.ch[u2][]]) {
t1.cnt[t1.ch[u1][]]--;
t2.cnt[t2.ch[u2][]]--;
u1 = t1.ch[u1][]; u2 = t2.ch[u2][];
} else if(t1.cnt[t1.ch[u1][]] && t2.cnt[t2.ch[u2][]]) {
t1.cnt[t1.ch[u1][]]--;
t2.cnt[t2.ch[u2][]]--;
u1 = t1.ch[u1][]; u2 = t2.ch[u2][];
} else if(t1.cnt[t1.ch[u1][]] && t2.cnt[t2.ch[u2][]]) {
t1.cnt[t1.ch[u1][]]--;
t2.cnt[t2.ch[u2][]]--;
u1 = t1.ch[u1][]; u2 = t2.ch[u2][];
x ^= ( << p);
} else {
t1.cnt[t1.ch[u1][]]--;
t2.cnt[t2.ch[u2][]]--;
u1 = t1.ch[u1][]; u2 = t2.ch[u2][];
x ^= ( << p);
} }
ans.push_back(x);
}
return ;
}
int main(){
int _;scanf("%d",&_);
while(_--){
int n;scanf("%d",&n);
t1.init(),t2.init();
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
t1.Insert(x);
}
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
t2.Insert(x);
}
ans.clear();
solve(n);
sort(ans.begin(),ans.end());
printf("%d",ans[]);
for(int i=;i<ans.size();i++)
{
printf(" %d",ans[i]);
}puts("");
}
}

HDU 6625 (01字典树)的更多相关文章

  1. three arrays HDU - 6625 (字典树)

    three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...

  2. HDU - 4825 01字典树套路题

    /*H E A D*/ struct Trie{ int son[maxn<<2][2]; int b[67],tot; void init(){ // memset(son,0,size ...

  3. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  4. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  5. hdu 4825 && acdream 1063 01字典树异或问题

    题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...

  6. HDU 5536 Chip Factory (暴力+01字典树)

    <题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...

  7. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

  8. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  9. Chip Factory---hdu5536(异或值最大,01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...

随机推荐

  1. 小白学习django第二站-模版配置

    上一站说道app创建,接下来我们来配置app的url路由 首先需要到setting.py中添加book这个app, 再到django_test文件里的urls添加路由 include() : 这个函数 ...

  2. The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I

    传送门 因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门 这里维护虚儿子用的是 $multiset$ ,没写可删堆 #include<i ...

  3. Gogs官方帮助文档

    环境要求 数据库(选择以下一项): MySQL:版本 >= 5.7 PostgreSQL MSSQL TiDB(实验性支持,使用 MySQL 协议连接) 或者 什么都不安装 直接使用 SQLit ...

  4. 【leetcode 136】136. Single Number

    要求:给定一个整数数组,除了其中1个元素之外,其他元素都会出现两次.找出这个只出现1次的元素. 例: array =[3,3,2,2,1]    找出元素1. 思路:最开始的想法是用两次for循环,拿 ...

  5. AndroidStudio Gradle手动下载和安装

    操作流程概述: 下载好的压缩包和解压后的文件夹复制到gradle-5.5.1-all --->97z1ksx6lirer3kbvdnh7jtjg文件夹下,将gradle-5.5.1-all.zi ...

  6. 基于SOA的图书商城系统分析

    1.1什么是SOA架构? SOA( Service Oriented Architecture)是一种面向服务的分布式架构,将每个实现特定功能的工程拆分为服务层和表现层.服务层负责处理业务逻辑,对外提 ...

  7. Python3使运行暂停的方法

    在Python3中已经有很大一部分语句与Python2不互通了,运行暂停的方法也有所不同. 1.input(); 这种方法不用包含模块,因此这也是最常用的一种暂停手段. Python2中的raw_in ...

  8. C Make a Square Educational Codeforces Round 42 (Rated for Div. 2) (暴力枚举,字符串匹配)

    C. Make a Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

  9. Python核心技术与实战——十五|深入了解迭代器和生成器

    我们在前面应该写过类似的代码 for i in [1,2,3,4,5]: print(i) for in 语句看起来很直观,很便于理解,比起C++或Java早起的 ; i<n;i++) prin ...

  10. .align 5 .MACRO .ENDM .word

    ARM的.align 5就是2的5次方对齐,也就是4字节对齐 .macro <name> {<arg_1} {,<arg_2>} … {,<arg_N>} 定 ...