题意:
给定两个长为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. hashlib模块和hmac模块

    hashlib模块和hmac模块 hashlib模块 一.导入方式 import hashlib 二.作用 无论你丢什么字符串,他都会返回一串 固定长度的字符串 三.模块功能 3.1 经常使用 imp ...

  2. docker无法删除<none>镜像

    .进入root权限 sudo su # 或 sudo -i .停止所有的container(这样才能够删除其中的images): docker stop $(docker ps -a -q) 如果想要 ...

  3. 无法安装 Microsoft Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持此安装。有关如何解决此问题的说明,请参阅 Microsoft 下载中心网站上的自述文件

    解决办法: 32 位系统删除:HKEY_LOCAL_MACHINE\Software\Microsoft\VSTO Designtime Setup\ 64 位系统删除:HKEY_LOCAL_MACH ...

  4. matlab 正弦信号产生

    fs=2400;%设定采样频率N=1000; %采样的点数n=0:N-1;t=n/fs; %1/fs相当于隔多长时间才一个点f1=50;%设定争先信号频率xn=sin(2*pi*f1*t);figur ...

  5. PAT Basic 1043 输出PATest (20 分)

    给定一个长度不超过 ​ ​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符 ...

  6. Ubuntu 16.04 编译ORB_SLAM2_modified

    编译g2o_with_orbslam2 1.修改g2o/types/slam2d/edge_se2_pointxy_bearing.cpp t.setRotation(t.rotation().ang ...

  7. SugarCRM开发入门

    SugarCRM官网下载地址:https://sourceforge.net/projects/sugarcrm/ 概述 Sugar最初是基于LAMP(Linux.Apache.MySQL和PHP)运 ...

  8. 跨域 (2) cors

    html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  9. 【ZOJ3627】Treasure Hunt II

    题目大意:给定一个长度为 N 的序列,现有两个人从 P 点出发,每个单位时间每个人最多可以移动一个单位,两人之间的最大距离不能超过 M,一共有 T 单位的时间,求在合法情况下,两人可以获得的序列点权和 ...

  10. web性能优化--减少客户端请求数(一)

    多图标合并,用css分隔 设置较长时间的过期时间 合并多个css文件 合并多个js文件 根据域名划分内容 首先介绍一款速度测试工具:webpagetest(填上url,username,passwor ...