题目链接: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字典树)的更多相关文章

  1. [2019杭电多校第五场][hdu6630]permutation 2

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6630 题意为求出1-n,n个数的全排列中有多少种方案满足第一位为x,第n位为y,且相邻数字绝对值之差不 ...

  2. [2019杭电多校第五场][hdu6624]fraction

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6624 题意为求最小的b满足$a*b^{-1}\equiv x(modp)$. 把式子化简一下: $a\ ...

  3. [2019杭电多校第五场][hdu6629]string matching(扩展kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6629 题意求字符串的每个后缀与原串的最长公共前缀之和. 比赛时搞东搞西的,还搞了个后缀数组...队友一 ...

  4. [2019杭电多校第五场][hdu6628]permutation 1

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6628 题意为求字典序第k小的差异数组,差异数组p满足p[i]=a[i+1]-a[i]. 头铁的爆搜,因 ...

  5. 2019杭电多校第五场 discrete logarithm problem

    https://vjudge.net/contest/317493#problem/I

  6. [2019杭电多校第三场][hdu6609]Find the answer(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...

  7. [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...

  8. 2019杭电多校第三场hdu6609 Find the answer(线段树)

    Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...

  9. 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)

    //never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...

随机推荐

  1. 错误消息对话框QErrorMessage

    继承于   QDialog 样式:   这个复选框的作用:文本框中相同信息时是否再显示 import sys from PyQt5.QtWidgets import QApplication, QWi ...

  2. logstash+redis收集负载均衡模式下多台服务器的多个web日志

    一.logstash的简介 一般我们看日志来解决问题的时候要么 tail+grep 要么 把日志下载下来再搜索,可以应付不多的主机和应用不多的部署场景.但对于多机多应用部署就不合适了.这里的多机多应用 ...

  3. Hybris commerce产品主数据的搜索API,批量返回若干主数据的值

    新建一个产品,identifier设置为i042416-1,创建之后立即能够在Backoffice里搜索出来: 等到Storefront的indexing做完之后,前台通过关键字i042416也能将这 ...

  4. LeetCode 1143 最长公共子序列

    链接:https://leetcode-cn.com/problems/longest-common-subsequence 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序 ...

  5. Quick BI支持哪些数据源(配置操作篇)

    Quick BI 潜心打造了核心技术底座(OLAP分析引擎),实现了SQL解析.SQL调度.SQL优化.查询加速等基础能力,支撑Quick BI的数据分析和查询加速.OLAP分析引擎包括数据源连接.数 ...

  6. PHP入门培训教程 php中的时间处理

    php中的时间处理 PHP入门培训教程 兄弟连PHP培训 小编整理的 php中的时间处理: <? /** * 转换为UNIX时间戳 */ function gettime($d) { if(is ...

  7. HDU 2037(贪心)

    “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了.  ...

  8. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  9. IO重定向与管道

    一.三种IO设备 程序:数据+指令 或 数据结构+算法 程序必须能够读入输入然后经过加工来产生结果,其接受的输入可以是变量.数组.列表.文件等等,生产出来的结果可以使变量.数组.列表.文件等等.即: ...

  10. hdu3714 Error Curves

    题目: Error Curves Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...