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]有值,那么就向下继续查找/ ...
随机推荐
- 一个好用且方便的FastCgi C++库 - FastCgi++
不知道你是不是曾经发愁过使用FastCgi库来使用C++开发Fastcgi程序繁琐而且会与C++ STL代码产生冲突的地方,或者你还是习惯了cout而不是pringf,那这篇文章就可以了解到一个使用的 ...
- 构建第一个Java程序
- HDFS 的可靠性
HDFS 的可靠性主要有一下几点: 冗余副本策略 机架策略 心跳机制 安全模式 效验和 回收站 元数据保护 快照机制 1.冗余副本策略 可以在hdfs-site.xml中设置复制因子指定副本数量 所有 ...
- 读取tiled地图
原地址:http://www.unity蛮牛.com/thread-20854-1-1.html Tile是一个非常好用的地图编辑器,一直以来我都在找支持tilemap的unity2D插件,但是找 ...
- CentOS 5: Make Command not Found
在centos 5下安装软件遇到的问题,google了一圈,是因为系统没有安装编译器,那安装就是了,嘿嘿. 解决办法,在SSH下输入下面的命令 yum -y install gcc automake ...
- 漫话C++0x(五)—- thread, mutex, condition_variable
熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...
- POJ3697+BFS+hash存边
/* 疾速优化+hash存边 题意:给定一个包含N(1 ≤ N ≤ 10,000)个顶点的无向完全图,图中的顶点从1到N依次标号.从这个图中去掉M(0 ≤ M ≤ 1,000,000)条边,求最后与顶 ...
- hdu 3400 Line belt 三分法
思路:要求最短时间从A到D,则走的路线一定是AB上的一段,CD上的一段,AB与CD之间的一段. 那么可以先三分得到AB上的一个点,在由这个点三分CD!! 代码如下: #include<iostr ...
- Spring RestTemplate介绍
http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...
- 对C#中的web访问mysql数据库的一些知识点进行了整理归纳总结
基本对比 使用方式 使用场合 优缺点 是否需要安装 需要的dll网址 引用方式 程序内引用 程序初期确定使用MySql,前期添加引用 大多数情况下使用在类文件内,多数使用于aspx,ashx等带有后置 ...