three arrays HDU - 6625 (字典树)
three arrays
\]
题意
给出 \(a\),\(b\) 数组,定义数组 \(c[i] = a[i] XOR b[i]\),现在可以任意调整 \(a\) 和 \(b\) 的顺序,使得最后的 \(c\) 字典序最小。
思路
对于 \(a\) 数组和 \(b\) 数组分别建立字典树,然后从大到小在字典树上贪心构造尽量小的 \(c\)。
对于某一位,如果两颗树上同时有 \(00\) 或者 \(11\),那么就选择往这样的边 \(dfs\),否则的话往 \(01\) 或者 \(10\) 去 \(dfs\),并加上这一位的异或值。
因为 \(01\) 和 \(10\) 不可能同时出现,因为如果同时出现了,肯定优先去 \(00\) 或者 \(11\)。那么就是 \(00\) 和 \(11\) 的情况,可能出现往 \(00\) 的答案是 \(4\),往 \(11\) 的答案是 \(3\),那么无法让小的先出来,所以可以任一选一个走,把另一个留到后面走,然后对于所有求出来的 \(c\) 在 \(sort\) 一遍。
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : Fri 06 Sep 2019 09:17:26 PM CST
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int tot[2];
int a[maxn], b[maxn], c[maxn];
int node[2][maxn*30][2], cnt[2][maxn*30];
void insert(int x, int id) {
int rt = 0;
for(int i=30; i>=1; i--) {
int f = (x&(1<<(i-1))) ? 1 : 0;
if(node[id][rt][f] == 0) {
tot[id]++;
mes(node[id][tot[id]], 0);
cnt[id][tot[id]] = 0;
node[id][rt][f] = tot[id];
}
rt = node[id][rt][f];
cnt[id][rt]++;
}
}
int dfs(int rt1, int rt2, int p) {
// printf("rt1=%d rt2=%d\n", rt1, rt2);
if(p == 0) return 0;
bool ok00 = (node[0][rt1][0] && cnt[0][node[0][rt1][0]]);
bool ok01 = (node[0][rt1][1] && cnt[0][node[0][rt1][1]]);
bool ok10 = (node[1][rt2][0] && cnt[1][node[1][rt2][0]]);
bool ok11 = (node[1][rt2][1] && cnt[1][node[1][rt2][1]]);
if(ok00 && ok10) {
cnt[0][node[0][rt1][0]]--, cnt[1][node[1][rt2][0]]--;
return dfs(node[0][rt1][0], node[1][rt2][0], p-1);
} else if(ok01 && ok11) {
cnt[0][node[0][rt1][1]]--, cnt[1][node[1][rt2][1]]--;
return dfs(node[0][rt1][1], node[1][rt2][1], p-1);
} else if(ok00 && ok11) {
cnt[0][node[0][rt1][0]]--, cnt[1][node[1][rt2][1]]--;
return dfs(node[0][rt1][0], node[1][rt2][1], p-1) + (1<<(p-1));
} else {
cnt[0][node[0][rt1][1]]--, cnt[1][node[1][rt2][0]]--;
return dfs(node[0][rt1][1], node[1][rt2][0], p-1) + (1<<(p-1));
}
}
int main() {
// freopen("in", "r", stdin);
scanf("%d", &T);
while(T--) {
cnt[0][0] = cnt[1][0] = tot[0] = tot[1] = 0;
mes(node[0][0], 0), mes(node[1][0], 0);
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%d", &a[i]), insert(a[i], 0);
for(int i=1; i<=n; i++) scanf("%d", &b[i]), insert(b[i], 1);
for(int i=1; i<=n; i++) c[i] = dfs(0, 0, 30);
sort(c+1, c+1+n);
for(int i=1; i<=n; i++) printf("%d%c", c[i], i==n ? '\n' : ' ');
}
return 0;
}
three arrays HDU - 6625 (字典树)的更多相关文章
- [2019杭电多校第五场][hdu6625]three arrays(01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典 ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- hdu 1251 字典树的应用
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...
- hdu 2896 字典树解法
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
随机推荐
- Ubuntu 18 Kubernetes集群的安装和部署 以及Helm的安装
首先说一下我的环境, 我是在windows 10 上面建了一个ubuntu18的虚拟机,同时由于某些原因 不受网络限制, 所以安装比较顺利. Install 1.安装并启用 Docker sudo ...
- CSP-S2019 自闭记
$Day0:$ 最后一场zr十连测从200挂到60,嘴上说着攒rp心里觉得药丸. 得知自己在本校考试感觉8错. $Day1:$ 早上7点50到了校门口,没让进QAQ早知道我再下一把棋了. 于是跟熊聊天 ...
- go中&^(按位置零)符号的含义
go中有一个 &^ 的运算符,它代表的是按位置零 首先来看下几个输出例子: i := 1 &^ 0 fmt.Println("1 &^ 0 -- ",i) ...
- 关于暗网需要关闭JS的处理
最近电视剧导致暗网热度很大,执法力度也大了很多,大部分暗网聚集地都不允许开JS权限访问(原因大家都懂,防止钓鱼执法) 因为是英文版而且是火狐,所以简单记录下,以防小白蛋疼 再打开就可以了 Tor协议 ...
- .NET / C# 时间与时间戳的转换
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数. 我们在计算时间戳时应为1970年01月01日到指定时间. 应当注 ...
- rest-spring-boot-starter
rest-spring-boot-starter 基于spring boot,统一业务异常处理,统一返回格式包装 依赖 <dependency> <groupId>tk.fis ...
- Vue学习之全局和私有组件小结(七)
一.组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用相应的组件即可. 二.组件和模块: 1.模块化:是从代码逻 ...
- mongoDB看这篇就够了
写在前面 hello,小伙伴们,我是 pubdreamcc ,本篇文章依旧出至于我的 GitHub仓库 node学习教程 ,如果你觉得我写的还不错,欢迎给个 star ,小伙伴们的 star 是我持续 ...
- Easy2game使用
每个独享IP服务器,开设3-5个高速接口IP,用户可自行选择当地连接速度快的接口接入服务器,服务器再为用户自动分配所绑定的独享IP连接至游戏服务器,可保证连接的稳定性,统一性 打开软件 添加程序 服务 ...
- Java String 字符串
equals 字符串比较 String str = "furong"; String str1 = new String("furong"); System.o ...