HDU 4825-Xor Sum(trie)
题意:
给你一组数,开始询问给一个数 求组中与该数异或值最大的数。
分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的位置(放便取) 对于每个询问 在搜树时优先考虑和当前数位相反的节点。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 4000010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct Trie{
ll ch[N][],num;
int val[N];
void init(){
num=;
memset(ch[],,sizeof(ch[]));
}
void build(ll x,int id){
int u=;
for(int i=;i>=;i--){
int v=(x&(1LL<<i))>;
if(!ch[u][v]){
memset(ch[num],,sizeof(ch[num]));
val[num]=;
ch[u][v]=num++;
}
u=ch[u][v];
}
val[u]=id;
}
int query(ll x){
int u=;
for(int i=;i>=;i--){
int v=(x&(1LL<<i))>;
if(ch[u][!v]){
u=ch[u][!v];
}
else{
u=ch[u][v];
}
}
return val[u];
}
}trie;
int main()
{
int t,n,m;
ll a[];
scanf("%d",&t);
for(int o=;o<=t;++o){
trie.init();
printf("Case #%d:\n",o);
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i){
scanf("%I64d",&a[i]);
trie.build(a[i],i);
}
ll s;
for(int i=;i<m;++i){
scanf("%I64d",&s);
printf("%I64d\n",a[trie.query(s)]);
}
}
return ;
}
HDU 4825-Xor Sum(trie)的更多相关文章
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- hdu 4825 Xor Sum (建树) 2014年百度之星程序设计大赛 - 资格赛 1003
题目 题意:给n个数,m次询问,每次给一个数,求这n个数里与这个数 异或 最大的数. 思路:建一个类似字典数的数,把每一个数用 32位的0或者1 表示,查找从高位向底位找,优先找不同的,如果没有不同的 ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- HDU 4825:Xor Sum(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意:给出N个数,M个询问,每个询问给出一个X,问在这N个数中哪个数和X异或后结果最大. 思路:可以用Tr ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 4825 Xor Sum(二进制的字典树,数组模拟)
题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #in ...
- HDU 4825 Xor Sum (裸字典树+二进制异或)
题目链接 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum(字典树)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...
随机推荐
- Linux find example
find | xargs echo >> x1 find -exec echo {} \; >> x2 1.查找/var目录下属主为root并且属组为mail的所有文件:fin ...
- BZOJ 4123 [Baltic2015] Hacker 解题报告
首先,Alice 会选择一个长度为 $\lfloor\frac{n+1}{2}\rfloor$ 的区间,我们把这个长度记为 $len$. 有这么一个结论:令 $F_i$ 为覆盖 $i$ 点的所有长度为 ...
- PAT-乙级-1010. 一元多项式求导 (25)
1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一 ...
- APP 上传之后出现"invalid binary" 问题解决汇总
背景 5.1 号开始 App 审核开始强制支持 iPhone5,并禁止使用 UDID. 问题 上传 app 后一直处于 Invalid Binary 状态,并且收到一封邮件说 Non-public A ...
- What is SuppressWarnings (“unchecked”) in Java?
ometime when looking through code, I see many methods specify an annotation: @SuppressWarnings(" ...
- Nagios3完整配置文档
第一章:简单快速安装nagios 1.1 准备软件包 在做安装之前确认要对该机器拥有root权限. 确认你安装好的linux系统上已经安装如下软件包再继续. Apache GCC编译器 GD库与开发库 ...
- [topcoder]ZigZag
http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 动态规划题.如果不用DP,暴力的应当在2^ ...
- 内核request_mem_region 和 ioremap的理解
request_mem_region仅仅是linux对IO内存的管理,意思指这块内存我已经占用了,别人就不要动了,也不能被swap出去.使用这些寄存器时,可以不调用request_mem_region ...
- Qt4.6.2已编译二进制版本在VS2005中的问题
结论1:如果你想把Qt4.6.2安装在VS2005中,又不想花时间编译,请下载和安装qt-win-opensource-4.6.2-vs2008,并单独编译“QT安装路径/src/winmain/” ...
- 强强合体:Docker版Kali Linux发布
Kali Linux是一款开源的基于Debian的渗透测试专用操作系统,系统中包含一系列用于渗透测试的神器.最近,Kali的开发者们为喜爱Docker的童鞋们发布了新版本. FreeBuf百科:什么是 ...