题目链接: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. matplotlib中 plt.plot() 函数中**kwargs的参数形式

    plt.plot(x, y, **kwargs) **kwargs的参数大致有如下几种: color: 颜色 linestyle: 线条样式 marker: 标记风格 markerfacecolor: ...

  2. WordPress显示评论者IP归属地、浏览器、终端设备、电信运营商

    在网上查资料闲逛,偶然间看到了张戈博客的评论框有点意思,于是就收走拿到了我的米扑博客. 本文为米扑博客原创:总结分享 WordPress显示评论者IP归属地.浏览器.终端设备.电信运营商 WordPr ...

  3. 微信小程序mpvue项目使用WuxWeapp前端UI组件

    前言:这是一篇简单粗暴的使用指南 在最近的小程序项目里前端UI框架最后选择使用WuxWeapp,这篇文章记录一下如何在小程序mpvue项目中使用该UI组件. 步骤一:下载源码 (地址在这里)主要是里面 ...

  4. AcWing:144. 最长异或值路径(dfs + 01字典树)

    给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...

  5. linux 查看系统性能

    1. 查看内存和CPU信息 cat /proc/cpuinfo                   cpu信息 cat /proc/meminfo |grep MemTotal    内存信息 查看物 ...

  6. hadoop+zookeeper+hbase分布式安装

    前期服务器配置 修改/etc/hosts文件,添加以下信息(如果正常IP) 119.23.163.113 master 120.79.116.198 slave1 120.79.116.23 slav ...

  7. Java读取根据HTML模板生成HTML页面

    首先,我们需要一个html模板: <html> <head> <title>###title###</title> <meta http-equi ...

  8. TCP发送窗口更新tcp_ack_update_window

    在tcp_ack接收ACK处理函数中,如果确认当前走慢速路径,那么会调用tcp_ack_update_window函数检查窗口是否需要更新并更新之,并且更新未确认数据的位置,即更新窗口左边沿: sta ...

  9. Button.OnClientClick

    Button.OnClientClick Gets or sets the client-side script that executes when a Button control's Click ...

  10. 执行docker run命令时报错Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

    一.解决办法: 修改host 二.步骤如下 2.1 安装dig工具  sudo apt-get install dnsutils -y (ubuntu下的安装方法) 2.2 找到registry-1. ...