题目链接: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. ValueError: Cannot assign "\<QuerySet [<Area: China>]\>": "Area.parent" must be a "Area" instance.

    在研究才Django自关联的过程中,在插入数据时爆出如下错误: ValueError: Cannot assign "<QuerySet [<Area: China>]&g ...

  2. ansible主机互信

    前文讲了ansible,但是ansible是基于ssh来做的,首先的和管理主机之间做主机互信,简单来说主机互信就是把主机上产生的公钥传到互信主机上就可以了. 在主机上产生公钥文件.使用命令:ssh-k ...

  3. func<T> 和 action<T>

    一.Func Func<Result>,Func<T1,Result>是一个.Net内置的泛型委托. Func<TResult> Func<T,TResult ...

  4. ARTS打卡计划第六周

    Algorithms: https://leetcode-cn.com/problems/longest-palindromic-substring/ 中心扩展法首先考虑,当然看到有个动态规划,一直很 ...

  5. 浅析SPDY

    1.什么是SPDY? 简单地说,在SSL层上增加一个SPDY会话层,以在一个TCP连接支持并发的HTTP请求.也就是他能通过复用仅仅一条(或几条)TCP连接,在客户端与服务器间发送几十个请求或回应. ...

  6. 普通线程类获取service,controller等spring容器类

    package com.zihexin.application.strategy; import org.springframework.beans.BeansException; import or ...

  7. JvmOverloads kotlin(14)(转)

    在Kotlin中@JvmOverloads注解的作用就是:在有默认参数值的方法中使用@JvmOverloads注解,则Kotlin就会暴露多个重载方法.可能还是云里雾里,直接上代码,代码解释一切:如果 ...

  8. C++中的to_string()

    目录 C++中的to_string() 注:原创不易,转载请务必注明原作者和出处,感谢支持! C++中的to_string() C++中的 to_string()系列函数将数值转换成字符串形式.注意, ...

  9. javascript之DOM四位的验证码简单实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 集成学习之Adaboost算法原理

    在boosting系列算法中,Adaboost是最著名的算法之一.Adaboost既可以用作分类,也可以用作回归. 1. boosting算法基本原理 集成学习原理中,boosting系列算法的思想: