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

题目链接: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一下就行了(答案都是会在里面的)。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>   http://acm.hdu.edu.cn/showproblem.php?pid=6625
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 void swapp(int &a,int &b);
 31 double fabss(double a);
 32 int maxx(int a,int b);
 33 int minn(int a,int b);
 34 int Del_bit_1(int n);
 35 int lowbit(int n);
 36 int abss(int a);
 37 //const long long INF=(1LL<<60);
 38 const double E=2.718281828;
 39 const double PI=acos(-1.0);
 40 const int inf=(1<<29);
 41 const double ESP=1e-9;
 42 const int N=(int)31e5+10;
 43 int read(){
 44     int x=0,f=1;char ch=getchar();
 45     for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1;
 46     for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;
 47     return x*f;
 48 }
 49 int n;
 50 struct Trie
 51 {
 52     int tr[N][2],val[N],sz[N][2],rt,tot;
 53     void init()
 54     {
 55         rt=tot=0;
 56         for(int i=0;i<=n*31;++i)
 57             tr[i][0]=tr[i][1]=val[i]=sz[i][0]=sz[i][1]=0;
 58     }
 59     void insert(int x)
 60     {
 61         rt=0;
 62         for(int i=30;i>=0;--i)
 63         {
 64             int id=(x>>i)&1;
 65             if(!tr[rt][id]) tr[rt][id]=++tot;
 66             ++sz[rt][id];
 67             rt=tr[rt][id];
 68         }
 69         val[rt]=x;
 70     }
 71 }a,b;
 72
 73 int ans[100005];
 74 int search(int rt1,int rt2)
 75 {
 76     while(1)//写成递归可能会TLE;
 77     {
 78         if(a.val[rt1]||b.val[rt2])
 79             return a.val[rt1]^b.val[rt2];
 80         if(a.sz[rt1][1]&&b.sz[rt2][1])// 有同11/00就走
 81         {
 82             --a.sz[rt1][1];--b.sz[rt2][1];
 83             rt1=a.tr[rt1][1],rt2=b.tr[rt2][1];
 84             continue;
 85         }
 86         if(a.sz[rt1][0]&&b.sz[rt2][0])
 87         {
 88             --a.sz[rt1][0];--b.sz[rt2][0];
 89             rt1=a.tr[rt1][0],rt2=b.tr[rt2][0];
 90             continue;
 91         }
 92         int to1,to2;
 93         to1=a.sz[rt1][0]?0:1;//实在没有相同的能走就走;
 94         to2=b.sz[rt2][0]?0:1;
 95         --a.sz[rt1][to1],--b.sz[rt2][to2];
 96         rt1=a.tr[rt1][to1],rt2=b.tr[rt2][to2];
 97     }
 98 }
 99
100 int main()
101 {
102     int T;
103     T=read();
104     while(T--)
105     {
106         n=read();
107         a.init();b.init();
108         for(int i=1;i<=n;++i)
109         {
110             int t;
111             t=read();
112             a.insert(t);
113         }
114         for(int i=1;i<=n;++i)
115         {
116             int t;
117             t=read();
118             b.insert(t);
119         }
120         for(int i=1;i<=n;++i)
121             ans[i]=search(0,0);
122         sort(ans+1,ans+1+n);
123         for(int i=1;i<=n;++i)
124             printf("%d%c",ans[i],i==n?‘\n‘:‘ ‘);
125     }
126     return 0;
127 }
128
129 /**************************************************************************************/
130
131 int maxx(int a,int b)
132 {
133     return a>b?a:b;
134 }
135
136 void swapp(int &a,int &b)
137 {
138     a^=b^=a^=b;
139 }
140
141 int lowbit(int n)
142 {
143     return n&(-n);
144 }
145
146 int Del_bit_1(int n)
147 {
148     return n&(n-1);
149 }
150
151 int abss(int a)
152 {
153     return a>0?a:-a;
154 }
155
156 double fabss(double a)
157 {
158     return a>0?a:-a;
159 }
160
161 int minn(int a,int b)
162 {
163     return a<b?a:b;
164 }

原文地址:https://www.cnblogs.com/--HPY-7m/p/11348794.html

时间: 2024-08-15 15:16:21

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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典序最小. 大致的做法就是用两个数组中的数字二进制 建两颗字典树,同时记录每个位置的个数.然后在两颗字典树上同时dfs,优先往0-0和1-1方向走,不能走再走0-1,1-0方向. 因为0-0和1-1两种情况不分先后,所以走出来的不一定是最小的,走完得到的c数组要排序. 1 #include<iostr

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

题目链接 传送门 题意 给你一棵无根树,要你寻找一个根节点使得在将一条边权变为\(0\)后,离树根最远的点到根节点的距离最小. 思路 本题和求树的直径很像,不过要记得的东西有点多,且状态也很多. \(fi[u][0]\)表示在\(u\)这个结点不删边沿着子树方向能到达的最远距离,\(se[u][0]\)为第二远,\(th[u][0]\)为第三远,\(fa[u][0]\)表示沿着父亲方向能到达的最远距离,第二维为\(1\)表示删一条边能到达的距离. 不删边的转移和求树的直径转移方程基本上是一样的,

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

K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明这个范围内存在第k小的数,r=mid,否则不存在,l=mid+1. 代码如下 #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; inline int read(){

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

Snowy Smile 题目传送门 解题思路 先把y离散化,然后把点按照x的大小进行排序,我们枚举每一种x作为上边界,然后再枚举其对应的每一种下边界.按照这种顺序插入点,这是一个压维的操作,即在线段树中的y位置加上其w,并利用线段树来更新动态的最大子段和. 代码如下 #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 2005; stru

2014百度之星资格赛—— Xor Sum(01字典树)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起

HDU 4825 Xor Sum 【01字典树】

题面: Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧么? Input 输入包含若干组测试数据,每组

HDU 4825 Xor Sum(01字典树入门题)

http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的数建立01字典树,从高位开始建树.对于每个询问,如果当前位置值为0,那么在字典树中,如果有1的值,那么就优先走1,否则再走0. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using names

【Hdu4825】Xor Sum(01字典树)

Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧么? Solution 01字典树入门题 Code #include

HDU - 4825 Xor Sum(01字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题目: Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟