HDU 4825 Xor Sum(01字典树)题解
思路:先把所有数字存进字典树,然后从最高位贪心。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const ll INF = 1e17;
using namespace std;
int ch[ * maxn][], tot;
ll val[ * maxn];
void init(){
tot = ;
memset(ch[], , sizeof(ch[]));
}
void Insert(ll x){
int root = ;
for(int i = ; i >= ; i--){
int u = (x >> i) & ;
if(ch[root][u] == ){
memset(ch[tot], , sizeof(ch[root]));
ch[root][u] = tot++;
}
root = ch[root][u];
}
val[root] = x;
}
ll query(ll x){
int root = ;
for(int i = ; i >= ; i--){
int u = (x >> i) & ;
if(ch[root][!u] != ){
root = ch[root][!u];
}
else root = ch[root][u];
}
return val[root];
}
int main(){
int T, Case = ;
scanf("%d", &T);
while(T--){
init();
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++){
ll x;
scanf("%lld", &x);
Insert(x);
} printf("Case #%d:\n", Case++);
while(m--){
ll x;
scanf("%lld", &x);
printf("%lld\n", query(x));
}
}
return ;
}
HDU 4825 Xor Sum(01字典树)题解的更多相关文章
- HDU 4825 Xor Sum(字典树)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...
- hdu 4825 && acdream 1063 01字典树异或问题
题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- Xor Sum 01字典树 hdu4825
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
随机推荐
- C++中位运算
简介 1 位逻辑运算符: & (位 “与”) and ----------------- 2个都为1 才是1-----------0^0 = 0 , 0^1 = 0, 1^0 = 0 ...
- AssetBoundle加载非预设资源
1.定义一个协程LoadNonObjFromAB IEnumerator LoadNonObjFromAB(string ABURL, GameObject go, string assetName) ...
- 【转】robotFramework 与testlink集成
场景: robotframework 执行完用例之后,将执行结果报至testlink. 方案1: 通过TestLink-API-Python-client中的RF关键字 每条用例执行完成之后根据状态进 ...
- linux 查看python安装路径,版本号
一.想要查看ubuntu中安装的python路径 方法一:whereis python 方法二:which python 二.想要查看ubuntu中安装的python版本号 python ...
- jsp无法访问
一直无法访问jsp: 由于Spring boot使用的内嵌的tomcat,而内嵌的tamcat是不支持jsp页面的,所有需要导入额外的包才能解决. <!-- 解决jsp无法访问 --> & ...
- 通过百度api 获取当前城市3种方法
方法一:function showLocation(data) { console.log(data.content); //alert(data.content.address_detail.cit ...
- 几种线程安全的Map解析
转载自 面试必问-几种线程安全的Map解析 HashMap线程安全的吗? Java中平时用的最多的Map集合就是HashMap了,它是线程不安全的. 看下面两个场景: 1.当用在方法内的局部变量时,局 ...
- Deprecated: getEntityManager is deprecated since Symfony 2.1
PHP5.3应用中,登陆后台管理时提示错误: Deprecated: getEntityManager is deprecated since Symfony 2.1. Use getManager ...
- kibana添加ES索引403错误解决
kibana添加ES索引时发现kibana添加索引不生效,没有创建成功只是一闪而过 查看控制台发现报错403 解决办法: curl -XPUT -H "Content-Type: appli ...
- The Little Prince-12/07
The Little Prince-12/07 "My little man, where do you come from? What is this ‘where I live,‘ of ...