双01字典树最小XOR(three arrays)--2019 Multi-University Training Contest 5(hdu杭电多校第5场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625
题意:
给你两串数 a串,b串,让你一一配对XOR使得新的 C 串字典序最小。
思路:
首先这边有两个问题:
1. 我要怎么知道这两个数配对是最优的:一开始我也不明白(以为选择会有后效性),其实很简单,对 a 里的一个X,在 b 的01树里跑到的最优解Y也一定就是 b 的这个Y在 a 的01树里跑到的最优解X。
2. 如果现在两颗树里跑到的点的下一个只有一种选择的话,肯定就接着跑,但是如果现在两个点的下两个点都有01,那大家跑0还是跑1呢?其实随便选一个就行了,因为你下一次还是会跑另一个数的,虽然不是每次都找到 C 串里的头一个最小值,但是跑完sort一下就行了(答案都是会在里面的)。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> http://acm.hdu.edu.cn/showproblem.php?pid=6625
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int N=(int)31e5+;
int read(){
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
return x*f;
}
int n;
struct Trie
{
int tr[N][],val[N],sz[N][],rt,tot;
void init()
{
rt=tot=;
for(int i=;i<=n*;++i)
tr[i][]=tr[i][]=val[i]=sz[i][]=sz[i][]=;
}
void insert(int x)
{
rt=;
for(int i=;i>=;--i)
{
int id=(x>>i)&;
if(!tr[rt][id]) tr[rt][id]=++tot;
++sz[rt][id];
rt=tr[rt][id];
}
val[rt]=x;
}
}a,b; int ans[];
int search(int rt1,int rt2)
{
while()//写成递归可能会TLE;
{
if(a.val[rt1]||b.val[rt2])
return a.val[rt1]^b.val[rt2];
if(a.sz[rt1][]&&b.sz[rt2][])// 有同11/00就走
{
--a.sz[rt1][];--b.sz[rt2][];
rt1=a.tr[rt1][],rt2=b.tr[rt2][];
continue;
}
if(a.sz[rt1][]&&b.sz[rt2][])
{
--a.sz[rt1][];--b.sz[rt2][];
rt1=a.tr[rt1][],rt2=b.tr[rt2][];
continue;
}
int to1,to2;
to1=a.sz[rt1][]?:;//实在没有相同的能走就走;
to2=b.sz[rt2][]?:;
--a.sz[rt1][to1],--b.sz[rt2][to2];
rt1=a.tr[rt1][to1],rt2=b.tr[rt2][to2];
}
} int main()
{
int T;
T=read();
while(T--)
{
n=read();
a.init();b.init();
for(int i=;i<=n;++i)
{
int t;
t=read();
a.insert(t);
}
for(int i=;i<=n;++i)
{
int t;
t=read();
b.insert(t);
}
for(int i=;i<=n;++i)
ans[i]=search(,);
sort(ans+,ans++n);
for(int i=;i<=n;++i)
printf("%d%c",ans[i],i==n?'\n':' ');
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
双01字典树最小XOR(three arrays)--2019 Multi-University Training Contest 5(hdu杭电多校第5场)的更多相关文章
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
- [2019杭电多校第五场][hdu6625]three arrays(01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典 ...
- 2019年杭电多校第三场 1011题Squrirrel(HDU6613+树DP)
题目链接 传送门 题意 给你一棵无根树,要你寻找一个根节点使得在将一条边权变为\(0\)后,离树根最远的点到根节点的距离最小. 思路 本题和求树的直径很像,不过要记得的东西有点多,且状态也很多. \( ...
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...
- [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...
- 2019杭电多校第六场hdu6638 Snowy Smile(线段树+枚举)
Snowy Smile 题目传送门 解题思路 先把y离散化,然后把点按照x的大小进行排序,我们枚举每一种x作为上边界,然后再枚举其对应的每一种下边界.按照这种顺序插入点,这是一个压维的操作,即在线段树 ...
- 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)
K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...
随机推荐
- 9030PCI CAN驱动开发点滴
1.配置EEPROM. 使用PlxMon打开9030,基本修改Spacex(0,1,2,3), Chip selectx(0,1,2,3), 中断状态INTCSR(0x74), 其他(0x78),详细 ...
- jQuery属性操作之类样式操作
类样式的操作:指对DOM属性className进行添加.移除操作.比如addClass().removeClass().toggleClass(). 1. addClass() 1.1 概述 $(se ...
- 【知识库】-数据库_MySQL 的七种 join
掘金作者:haifeisi 文章出处: MySQL 的七种 join Learn [已经过测试校验] 一.内连接 二.左外连接 三.右外连接 四.左连接 五.右连接 六.全连接 七.两张表中都没有出现 ...
- HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)
参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...
- Inter IPP+ VS + opencv 在 Windows下的环境搭建
首先Inter官网申请和下载:https://software.intel.com/en-us/intel-ipp 需要VS2013或更高版本(先装vs再装IPP,我的版本是VS2015社区版,IPP ...
- vue 使用axios 出现跨域请求的两种解决方法
最近在使用vue axios发送请求,结果出现跨域问题,网上查了好多,发现有好几种结局方案. 1:服务器端设置跨域 header(“Access-Control-Allow-Origin:*”); h ...
- 普通线程类获取service,controller等spring容器类
package com.zihexin.application.strategy; import org.springframework.beans.BeansException; import or ...
- SpringBoot Thymeleaf 配置多个Template Locations
@Configuration public class ThymeleafConfigration { @Bean public SpringResourceTemplateResolver firs ...
- html上传图片后,在页面显示上传的图片
html上传图片后,在页面显示上传的图片 1.html <form class="container" enctype="multipart/form-data&q ...
- HearthBuddy的狂野和休闲模式来回切换
表现1 配置是标准,休闲模式 然后一直重复提示 select desire deck select causal mode 表现2 配置是狂野,休闲模式 然后一直提示 切换到狂野 切换到标准 把模式切 ...