HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825
Xor Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 3839 Accepted Submission(s): 1685
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
3 2
3 4 5
1
5
4 1
4 6 5 6
3
4
3
Case #2:
4
题解:
将所有数按二进制从高位到低位插入到Trie树中,然后再枚举每一个数,去Trie中反向匹配即可。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; struct Trie
{
int next[*MAXN][];
int root, L = ;
int newnode()
{
next[L][] = next[L][] = -;
L++;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(LL val)
{
int tmp = root, way;
for(int i = ; i>=; i--)
{
way = (val>>i)&;
if(next[tmp][way]==-) next[tmp][way] = newnode();
tmp = next[tmp][way];
}
}
LL query(LL val)
{
LL ret = ;
int tmp = root, way;
for(int i = ; i>=; i--)
{
ret <<= ;
way = (val>>i)&;
if(next[tmp][!way]!=-) //如果另一方向存在,则走另一方向
ret ^= !way, tmp = next[tmp][!way];
else //否则,顺着走
ret ^= way, tmp = next[tmp][way];
}
return ret;
}
};
Trie T; int main()
{
int n, m, TT;
scanf("%d",&TT);
for(int kase = ; kase<=TT; kase++)
{
scanf("%d%d",&n,&m);
T.init();
for(int i = ; i<=n; i++)
{
LL val;
scanf("%lld",&val);
T.insert(val);
} printf("Case #%d:\n",kase);
while(m--)
{
LL val;
scanf("%lld",&val);
printf("%lld\n", T.query(val));
}
}
}
HDU4825 Xor Sum —— Trie树的更多相关文章
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- HDU-4825 Xor Sum,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
随机推荐
- ElasticSearch命令增加字段总结
1.建立一个String类型的字段 curl -XPUT http://192.168.46.163:9200/t_risk_case/_mapping/t_risk_case?pretty -d ' ...
- VS2010 + C#4.0使用 async + await
方法一: 安装官方出的Microsoft.Bcl.Async包 最新发布日期为 2014/4/12,版本1.0.168 (不支持VS2010) 1.解决方案-右键-管理解决方案的NuGet程序包 2. ...
- Android设计中的尺寸问题
Android把屏幕大小分成四种:small, normal, large, xlarge; 屏幕密度分成:low(ldpi), medium(mdpi), high(hdpi), extra hig ...
- Iowait的成因、对系统影响及对策
什么是iowait?顾名思义,就是系统因为io导致的进程wait.再深一点讲就是:这时候系统在做io,导致没有进程在干活,cpu在执行idle进程空转,所以说iowait的产生要满足两个条件,一是进程 ...
- InnoDB Insert(插入)操作(下)--mysql技术内幕
接上一篇文章,最后做的那个实验,我是想证明mysql innodb存储引擎,commit操作与flush数据到磁盘之间的关系,当与同事交流之后,他说,你应该把innodb_buffer_size的大小 ...
- Java中使用com.sun相关jar包出现编译错误,但是运行没有错误的解决方法和原因
[解决方法]如果你用的是Eclipse 在preference->java->complier->errors/warning->deprecated and restrict ...
- Xshell 一款很养眼的配色方案推荐
Xshell 是个很好用的在 windows 下登陆 liunx 的终端原生支持中文,配合 Xftp 管理文件,同是免费软件可远比 Putty 好用多了面对枯燥的代码,我们需要一款很养眼的配色方案来保 ...
- Lua学习一----------开发环境搭建
© 版权声明:本文为博主原创文章,转载请注明出处 1.LuaDist下载地址:http://luadist.org/ 2.LuaRocks下载地址:https://github.com/luarock ...
- 【7.1.1】ELK集群搭建 之 ES集群
写在前边 昨天晚上就已经完成这篇博客了,就是在测试这块是否正常跑起来,晚上没搞完,上班前把电脑关机带着,结果没保存!基本上昨天写的东西都丢了,好在博客园的图片url还在. 为了让大家都轻松些,我轻松写 ...
- OpenStack安装CentOS镜像:Device eth0 does not seem to be present, delaying initialization
解决办法:删除 /etc/udev/rules.d/70-persistent-net.rules 后重启机器.70-persistent-net.rules这个文件确定了网卡与MAC地址的绑定,cl ...