题意:
给定两个长为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("");
}
}

最新文章

  1. Linux内核目录结构
  2. JavaScriptCore 使用
  3. 【BZOJ】【1017】【JSOI2008】魔兽地图Dotr
  4. C# 好书一本推荐
  5. Tolerance (定义发票允差)
  6. vim的配置
  7. python 试题
  8. ASP.NET 文件上传的实现(Upload)
  9. React-router4 简单总结
  10. web3.js编译Solidity,发布,调用全部流程(手把手教程)
  11. hive 踩坑
  12. Android-Java单例模式
  13. Mybatis(五):Mybatis的三种使用方式
  14. CentOS基础命令大全
  15. Eclipse上配置btm
  16. Pytorch permute,contiguous
  17. Spring + MySQL + Mybatis + Redis【二级缓存】
  18. 如何把SSL公钥和私钥转化为PFX格式
  19. [P3812][模板]线性基
  20. Android的file文件操作详解

热门文章

  1. 双连通分量(点-双连通分量&amp;边-双连通分量)
  2. centos:mysql主从同步配置(2018)
  3. 使用html2canvas实现屏幕截图
  4. s:schema报错问题
  5. 从零开始学MySQL(三)
  6. 基于zynq XC7Z100 FMC接口通用计算平台 XC7Z100
  7. idea 导出可以直接运行的jar 文件
  8. 如何使用 vue-cli 3 的 preset 打造基于 git repo 的前端项目模板
  9. Java 11必掌握的8大特性,完美代码信手拈来
  10. git提示Please enter a commit message to explain why this merge is necessary