[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 ...
随机推荐
- puppet使用rsync模块
puppet使用rsync模块同步目录和文件 环境说明: OS : CentOS5.4 i686puppet版本: ...
- Python---进阶---文件操作---比较文件不同
一.编写一个程序,接受用户输入的内容,并且保存为新的文件 如果用户单独输入:w 表示文件保存退出 --------------------------------------------- file_ ...
- 【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...
- 前端面试题常考&必考之--盒子模型和box-sizing(项目中经常使用)
主要考察width的值,包括padding\border\content等属性??? box-sizing属性是css3特有的哦*** 1>当box-sizing:content-box;时,跟 ...
- CF1051F The Shortest Statement Dijkstra + 性质分析
动态询问连通图任意两点间最短路,单次询问. 显然,肯定有一些巧妙地性质(不然你就发明了新的最短路算法了233)有一点很奇怪:边数最多只比点数多 $20$ 个,那么就可以将这个图看作是一个生成树,上面连 ...
- Qt中图元对象的多重集成
在继承自定义QGraphicsItem图元对象时,有时需要用到信号/槽机制,由于QGraphicsItem非QObject的子类 所以需要多重继承QObject,有一点需要特别注意:就是继承的顺序,一 ...
- 电脑新安装JDK版本并运行使用该JDK版本问题
情景:电脑上已正常安装一个jdk版本,如:1.7.0_71,因考虑到一些情况,现需要使用版本为1.7.0_80(1.8),故需新安装JDK,并使服务可以运行使用新安装的JDK版本. 网络找寻方法: ( ...
- jQuery easing动画效果扩展
引入Easing js文件 <script src="js/jquery.min.js"></script> <script src="js ...
- 基于GTID模式MySQL主从复制
基于GTID模式MySQL主从复制 GTID复制原理:基于GTID的复制是MySQL 5.6后新增的复制方式GTID (global transaction identifier) 即全局事务ID, ...
- Powershell 音乐播放
目录 目录 前言 systemwindowsmediamediaplayer 前言 Powershell抱着.NET的大腿,与生俱来了许多非常便捷的功能.例如--音乐的自动播放 system.wind ...