题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625

题意:

给你两串数 a串,b串,让你一一配对XOR使得新的 C 串字典序最小。

思路:

首先这边有两个问题:

1. 我要怎么知道这两个数配对是最优的:一开始我也不明白(以为选择会有后效性),其实很简单,对 a 里的一个X,在 b 的01树里跑到的最优解Y也一定就是 b 的这个Y在 a 的01树里跑到的最优解X。

2. 如果现在两颗树里跑到的点的下一个只有一种选择的话,肯定就接着跑,但是如果现在两个点的下两个点都有01,那大家跑0还是跑1呢?其实随便选一个就行了,因为你下一次还是会跑另一个数的,虽然不是每次都找到 C 串里的头一个最小值,但是跑完sort一下就行了(答案都是会在里面的)。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> http://acm.hdu.edu.cn/showproblem.php?pid=6625
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int N=(int)31e5+;
int read(){
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
return x*f;
}
int n;
struct Trie
{
int tr[N][],val[N],sz[N][],rt,tot;
void init()
{
rt=tot=;
for(int i=;i<=n*;++i)
tr[i][]=tr[i][]=val[i]=sz[i][]=sz[i][]=;
}
void insert(int x)
{
rt=;
for(int i=;i>=;--i)
{
int id=(x>>i)&;
if(!tr[rt][id]) tr[rt][id]=++tot;
++sz[rt][id];
rt=tr[rt][id];
}
val[rt]=x;
}
}a,b; int ans[];
int search(int rt1,int rt2)
{
while()//写成递归可能会TLE;
{
if(a.val[rt1]||b.val[rt2])
return a.val[rt1]^b.val[rt2];
if(a.sz[rt1][]&&b.sz[rt2][])// 有同11/00就走
{
--a.sz[rt1][];--b.sz[rt2][];
rt1=a.tr[rt1][],rt2=b.tr[rt2][];
continue;
}
if(a.sz[rt1][]&&b.sz[rt2][])
{
--a.sz[rt1][];--b.sz[rt2][];
rt1=a.tr[rt1][],rt2=b.tr[rt2][];
continue;
}
int to1,to2;
to1=a.sz[rt1][]?:;//实在没有相同的能走就走;
to2=b.sz[rt2][]?:;
--a.sz[rt1][to1],--b.sz[rt2][to2];
rt1=a.tr[rt1][to1],rt2=b.tr[rt2][to2];
}
} int main()
{
int T;
T=read();
while(T--)
{
n=read();
a.init();b.init();
for(int i=;i<=n;++i)
{
int t;
t=read();
a.insert(t);
}
for(int i=;i<=n;++i)
{
int t;
t=read();
b.insert(t);
}
for(int i=;i<=n;++i)
ans[i]=search(,);
sort(ans+,ans++n);
for(int i=;i<=n;++i)
printf("%d%c",ans[i],i==n?'\n':' ');
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

双01字典树最小XOR(three arrays)--2019 Multi-University Training Contest 5(hdu杭电多校第5场)的更多相关文章

  1. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  2. [2019杭电多校第五场][hdu6625]three arrays(01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典 ...

  3. 2019年杭电多校第三场 1011题Squrirrel(HDU6613+树DP)

    题目链接 传送门 题意 给你一棵无根树,要你寻找一个根节点使得在将一条边权变为\(0\)后,离树根最远的点到根节点的距离最小. 思路 本题和求树的直径很像,不过要记得的东西有点多,且状态也很多. \( ...

  4. [2019杭电多校第三场][hdu6609]Find the answer(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...

  5. 杭电多校第四场 E Matrix from Arrays

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  6. [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...

  7. [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...

  8. 2019杭电多校第六场hdu6638 Snowy Smile(线段树+枚举)

    Snowy Smile 题目传送门 解题思路 先把y离散化,然后把点按照x的大小进行排序,我们枚举每一种x作为上边界,然后再枚举其对应的每一种下边界.按照这种顺序插入点,这是一个压维的操作,即在线段树 ...

  9. 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)

    K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...

随机推荐

  1. linux C线程

    一个应用程序可以启动若干个线程: 线程,是程序执行的最小单位: 一般一个最简单的程序最少有一个线程,就是程序本身,也是主函数: 一个线程阻塞不会影响另一个线程: 多线程的进程可以尽可能多的利用系统CP ...

  2. ast.literal_eval(转)

    eval函数在Python中做数据类型的转换还是很有用的.它的作用就是把数据还原成它本身或者是能够转化成的数据类型.那么eval和ast.literal_val()的区别是什么呢?本文将大家介绍关于P ...

  3. Java内存缓存-通过Map定制简单缓存

    缓存 在程序中,缓存是一个高速数据存储层,其中存储了数据子集,且通常是短暂性存储,这样日后再次请求此数据时,速度要比访问数据的主存储位置快.通过缓存,可以高效地重用之前检索或计算的数据. 为什么要用缓 ...

  4. JVM 监控工具——jstack

    [参考文章]:jstack 命令使用经验总结 1. 简介 jstack主要用于生成java虚拟机当前时刻的线程快照. 线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合, 主要目的是定位 ...

  5. LeetCode 80. 删除排序数组中的重复项 II(Remove Duplicates from Sorted Array II)

    题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  6. kentico中page alias的使用

    这里设置的path or pattern,是针对于根目录而言的

  7. Java-GC 垃圾收集器(HotSpot)

    垃圾收集器为垃圾收集算法的具体实现,是执行垃圾收集算法的,是守护线程. HotSpot 虚拟机采用分代收集(JVM 规范并未对堆区进行划分),将堆分为年轻代和老年代,垃圾收集器也按照这样区分.不过已有 ...

  8. 一、基础篇--1.1Java基础-Java运算符优先级

    在一个表达式中可能包含多个有不同运算符连接起来的.具有不同数据类型的数据对象:由于表达式有多种运算,不同的结合顺序可能得出不同结果甚至出现错误运算错误,因为当表达式中含多种运算时,必须按一定顺序进行结 ...

  9. jxbrowser java代码直接调用js代码

    https://blog.csdn.net/shuaizai88/article/details/73743669 final Browser browser = new Browser(); Bro ...

  10. Linux下深度学习常用工具的安装

    .Matlab 2015 64bit 的安装 (一)安装包下载 百度网盘: [https://pan.baidu.com/s/1gf9IeCN], 密码: 4gj3 (二)Vmware 使用Windo ...