[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数组要排序。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = + ;
struct Trie {
int tree[maxn * ][], num[maxn * ];
int len, root;
int newcode() {
tree[len][] = tree[len][] = ;
num[len] = ;
return len++;
}
void init() {
len = ;
root = newcode();
}
void insert(int x) {
int now = root;
for (int i = ; i >= ; i--) {
int k = (x >> i) & ;
if (!tree[now][k])
tree[now][k] = newcode();
now = tree[now][k];
num[now]++;
}
}
}A, B;
int ans[maxn];
void slove(int n) {
for (int i = ; i <= n; i++) {
ans[i] = ;
int nowA = , nowB = ;
for (int j = ; j >= ; j--) {
if (A.num[A.tree[nowA][]] && B.num[B.tree[nowB][]]) {
nowA = A.tree[nowA][], nowB = B.tree[nowB][];
A.num[nowA]--, B.num[nowB]--;
}
else if (A.num[A.tree[nowA][]] && B.num[B.tree[nowB][]]) {
nowA = A.tree[nowA][], nowB = B.tree[nowB][];
A.num[nowA]--, B.num[nowB]--;
}
else if (A.num[A.tree[nowA][]] && B.num[B.tree[nowB][]]) {
nowA = A.tree[nowA][], nowB = B.tree[nowB][];
A.num[nowA]--, B.num[nowB]--;
ans[i] += ( << j);
}
else if (A.num[A.tree[nowA][]] && B.num[B.tree[nowB][]]) {
nowA = A.tree[nowA][], nowB = B.tree[nowB][];
A.num[nowA]--, B.num[nowB]--;
ans[i] += ( << j);
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, x;
A.init(), B.init();
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &x), A.insert(x);
for (int i = ; i <= n; i++)
scanf("%d", &x), B.insert(x);
slove(n);
sort(ans + , ans + + n);
for (int i = ; i <= n; i++)
printf("%d%c", ans[i], i == n ? '\n' : ' ');
} }
[2019杭电多校第五场][hdu6625]three arrays(01字典树)的更多相关文章
- [2019杭电多校第五场][hdu6630]permutation 2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6630 题意为求出1-n,n个数的全排列中有多少种方案满足第一位为x,第n位为y,且相邻数字绝对值之差不 ...
- [2019杭电多校第五场][hdu6624]fraction
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6624 题意为求最小的b满足$a*b^{-1}\equiv x(modp)$. 把式子化简一下: $a\ ...
- [2019杭电多校第五场][hdu6629]string matching(扩展kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...
- [2019杭电多校第五场][hdu6628]permutation 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6628 题意为求字典序第k小的差异数组,差异数组p满足p[i]=a[i+1]-a[i]. 头铁的爆搜,因 ...
- 2019杭电多校第五场 discrete logarithm problem
https://vjudge.net/contest/317493#problem/I
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
- [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...
- 2019杭电多校第三场hdu6609 Find the answer(线段树)
Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
随机推荐
- 1--面试总结-js深入理解,对象,原型链,构造函数,执行上下文堆栈,执行上下文,变量对象,活动对象,作用域链,闭包,This
参考一手资料:http://dmitrysoshnikov.com/ecmascript/javascript-the-core/中文翻译版本:https://zhuanlan.zhihu.com/p ...
- pip install mysql_python报错解决办法
首先请注意,mysql_python只支持Python2,所以假如你是python3,就直接用python-connector去吧.下面这一条命令就可以了 pip install mysql-conn ...
- springcloud 配置actuator
pom.xml <!--Spring Boot Actuator,感应服务端变化--> <dependency> <groupId>org.springframew ...
- GitLab5
GitLab5发布快一个月了,决定试用下,5.0最大的特性就是用GitLab-Shell取代了Gitolite,这大大降低了安装难度,不多本人在安装过程中还是越到了一些问题,所以记录下来供要安装Git ...
- 【GDOI2014模拟】服务器
前言 直到比赛最后几分钟,才发现60%数据居然是一个水dp,结果没打完. 题目 我们需要将一个文件复制到n个服务器上,这些服务器的编号为S1, S2, -, Sn. 首先,我们可以选择一些服务器,直接 ...
- webkit内核的浏览器常见7种分别是..
Google Chrome Safari 遨游浏览器 3.x 搜狗浏览器 阿里云浏览器 QQ浏览器 360浏览器 ...
- 14 Spring Boot Shiro限制登录尝试次数
- break、continue、return的使用
跳转控制语句: java中的goto是保留字,目前不能使用,虽然没有了goto语句可以增强程序的安全性,但是也带来很多不便. 比如说:我们想让某个循环到某一步的时候就结束,现在就做不了这个事情了.为了 ...
- CF1051F The Shortest Statement Dijkstra + 性质分析
动态询问连通图任意两点间最短路,单次询问. 显然,肯定有一些巧妙地性质(不然你就发明了新的最短路算法了233)有一点很奇怪:边数最多只比点数多 $20$ 个,那么就可以将这个图看作是一个生成树,上面连 ...
- Shell的简单介绍(一)
shell 的分类 Shell 类别 易学性 可移植性 编辑性 快捷性 Bourne Shell (sh) 容易 好 较差 较差 Korn Shell (ksh) 较难 较好 好 较好 Bourne ...