hdu 6625 three array (01-trie)
大意: 给两个数组$a,b$, 要求重排使得$c_i=a_i\oplus b_i$字典序最小.
字典树上贪心取$n$次, 然后排序, 还不知道怎么证.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
const int N = 1e5+50, S = 29;
int n, T[2], tot;
struct {int ch[2];} tr[N*60];
int cnt[N*60][2];
vector<int> ans;
void add(int &o, int d, int x, int tp, int v) {
if (!o) o=++tot;
cnt[o][tp]+=v;
if (d>=0) add(tr[o].ch[x>>d&1],d-1,x,tp,v);
}
void solve(int u, int v, int d, int x, int y) {
--cnt[u][0],--cnt[v][1];
if (d<0) return ans.pb(x^y);
int A=cnt[tr[u].ch[0]][0],B=cnt[tr[u].ch[1]][0];
int C=cnt[tr[v].ch[0]][1],D=cnt[tr[v].ch[1]][1];
if (A&&C) return solve(tr[u].ch[0],tr[v].ch[0],d-1,x,y);
if (B&&D) return solve(tr[u].ch[1],tr[v].ch[1],d-1,x^1<<d,y^1<<d);
if (A&&D) return solve(tr[u].ch[0],tr[v].ch[1],d-1,x,y^1<<d);
if (B&&C) return solve(tr[u].ch[1],tr[v].ch[0],d-1,x^1<<d,y);
} void work() {
scanf("%d", &n);
REP(z,0,1) REP(i,1,n) {
int t;
scanf("%d", &t);
add(T[z],S,t,z,1);
}
ans.clear();
REP(i,1,n) solve(T[0],T[1],S,0,0);
sort(begin(ans),end(ans));
REP(i,0,n-1) printf("%d%c",ans[i]," \n"[i==n-1]);
T[0]=T[1]=0;
while (tot) cnt[tot][0]=cnt[tot][1]=tr[tot].ch[0]=tr[tot].ch[1]=0,--tot;
} int main() {
int t;
scanf("%d", &t);
while (t--) work();
}
hdu 6625 three array (01-trie)的更多相关文章
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- [一本通学习笔记] 字典树与 0-1 Trie
字典树中根到每个结点对应原串集合的一个前缀,这个前缀由路径上所有转移边对应的字母构成.我们可以对每个结点维护一些需要的信息,这样即可以去做很多事情. #10049. 「一本通 2.3 例 1」Phon ...
- 可持久化0-1 Trie 简介
Trie树是字符串问题中应用极为广泛的一种数据结构,可以拓展出AC自动机.后缀字典树等实用数据结构. 然而在此我们考虑0-1 Trie的应用,即在序列最大异或问题中的应用. 这里的异或是指按位异或.按 ...
- Hash Array Mapped Trie
Hash Array Mapped Trie Python\hamt.c
- HDU 6625 (01字典树)
题意:给定两个长为n的数组a和b:重新排列a和b,生成数组c,c[i]=a[i] xor b[i]:输出字典序最小的c数组. 分析:将a中的数插入一颗01字典树a中:将b中的数插入一颗01字典树b中: ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 2846:Repository(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- CMU Database Systems - Storage and BufferPool
Database Storage 存储分为volatile和non-volatile,越快的越贵越小 那么所以要解决的第一个问题就是,如果尽量在有限的成本下,让读写更快些 意思就是,尽量读写volat ...
- https://docs.huihoo.com/infoq/qconbeijing/2016/day1/工程效率提升专题/2-2-支持百度万人开发的工具装备及百度工程能力建设-夏仲璞.pdf
https://docs.huihoo.com/infoq/qconbeijing/2016/day1/工程效率提升专题/2-2-支持百度万人开发的工具装备及百度工程能力建设-夏仲璞.pdf http ...
- TreeFrog Framework : High-speed C++ MVC Framework for Web Application http://www.treefrogframework.org
TreeFrog Framework : High-speed C++ MVC Framework for Web Application http://www.treefrogframework.o ...
- 为什么要监控sql语句?如何监控?
01 为什么要监控sql语句? ① 因为程序大了以后,sql语句有可能被多个地方调用 .你不能确认当前时间是不是只执行了你需要的那条语句 . ② 有的持久层框架采用linq的语法来写sql , 程序中 ...
- 【opencv】opencv函数isContinuous
isContinuous 参考 1. opencv_isContinuous; 完
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 系统调用system call以及strace/dtruss
计算机系统的各种硬件资源是有限的,在现代多任务操作系统上同时运行的多个进程都需要访问这些资源,进程是不允许直接操作这些资源的,所有对这些资源的访问都必须有操作系统控制.也就是说操作系统是使用这些资源的 ...
- 什么是 CDN(超形象)
原文地址:https://blog.csdn.net/lu_embedded/article/details/80519898 618电商节.双十一购物狂欢节,到底是什么在支撑数以万计的秒杀活动?这就 ...
- VC.DNS解析(winsock)
1.尝试了 gethostbyname(...) 和 Qt598中的qhostinfo::fromname(...),都可以解析出来IP,但是有一个问题:域名指向的IP改变了,这两种方式需要等较长时间 ...