题目链接: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. delphi通过url下载文件

    procedure TfrmEngineerImport.btnDownloadClick(Sender: TObject);var vsql, SourceFile, DestFile, filen ...

  2. ssh以及双机互信

    当我们要远程到其他主机上面时就需要使用ssh服务了. 我们就来安装一下sshd服务以及ssh命令的使用方法. 服务安装: 需要安装OpenSSH 四个安装包: 安装包: openssh-5.3p1-1 ...

  3. 「Luogu P5603」小O与桌游

    题目链接 戳我 \(Solution\) 我们来分析题目. 实际上就是求一个拓扑序满足拓扑序的前缀最大值最多/最少 对于第一种情况,很明显一直选当前能选的最小的是最优的对吧.因为你需要大的尽可能多.用 ...

  4. TCP输入 之 tcp_rcv_established

    概述 tcp_rcv_established用于处理已连接状态下的输入,处理过程根据首部预测字段分为快速路径和慢速路径: 1. 在快路中,对是有有数据负荷进行不同处理: (1) 若无数据,则处理输入a ...

  5. java获取本机mac物理地址

    package com.simonjia.util.other; import java.net.InetAddress;import java.net.InterfaceAddress;import ...

  6. linux系统空间不足,lsof看到异常的delete状态的文件。

    #20191101更新---这篇文章适用于产生僵尸文件的进程是可kill的状态参考,就是这个进程死亡不影响业务,那么另外一种情况,也是我现在管理的项目中生产环境中出现过的情况,产生僵尸文件的进程是we ...

  7. ipv4 ipv6 求字符串和整数一一映射的算法 AmazonOrderId

    字符串和整数一一映射的算法 公司每人的英文名不同,现在给每个英文名一个不同的数字编号,怎么设计? 走ipv4/6  2/32 2/128就够了,把“网段”概念对应到“表或库”,ip有a_e5类,这概念 ...

  8. openerp学习笔记 搜索视图(自己创建的、自己的、本部门的、本部门及下属部门的、今日的、日期从,日期至、多条件模糊搜索、or、and)

    自己创建的: domain="[('create_uid','=',uid)]" 自己的: domain="[('employee_id','=','#kl_user_e ...

  9. Struts2中国际化

    1.  写资源文件 Msg.properties   默认的语言环境: 找不到配置就找它 Msg_en_US.properties  美国 2.  加载 <constant name=" ...

  10. Host key verification failed

    一.发现问题 问题如下图代码: 这里面,有一句很关键. ECDSA host key for 108.61.163.242 has changed and you have requested str ...