Xor Sum(HDU4825 + 字典树)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4825
题目:


题意:
先给你n个数,再进行q次查询,每次查询数s与原来给的n个数异或和最大的数。
思路:
建一棵字典树,将n个数转换成二进制存进去,每次查询时,对s的每一位进行匹配(从高位开始匹配,毕竟你低位再大也没有一位高位大效果好,如34 <42),匹配第i位时为了值尽可能大应先匹配1-a[i]再匹配a[i]。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); const double eps = 1e-;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t, n, q, le, root;
LL x;
int a[]; struct node{
int cnt;
int nxt[]; void init(){
cnt = ;
nxt[] = nxt[] = -;
}
}T[maxn*]; void insert(LL n){
int now = root;
for(int i = ; i <= ; i++) {
a[i] = n % ;
n /= ;
}
for(int i = ; i >= ; i--){
int x = a[i];
if(T[now].nxt[x] == -){
T[le].init();
T[now].nxt[x] = le++;
}
now = T[now].nxt[x];
T[now].cnt++;
}
} LL search(LL n){
int now = root;
LL ans = ;
for(int i = ; i <= ; i++) {
a[i] = n % ;
n /= ;
}
for(int i = ; i >= ; i--){
int x = a[i];
if(T[now].nxt[ - x] != -) {
if(x == ) ans += 1LL << i;
now = T[now].nxt[-x];
} else {
if(x == ) ans += 1LL << i;
now = T[now].nxt[x];
}
}
return ans;
} int main() {
int icase = ;
scanf("%d", &t);
while(t--) {
le = ;
T[].init();
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++) {
scanf("%lld", &x);
insert(x);
}
printf("Case #%d:\n", ++icase);
for(int i = ; i <= q; i++) {
scanf("%lld", &x);
printf("%lld\n", search(x));
}
}
return ;
}
Xor Sum(HDU4825 + 字典树)的更多相关文章
- HDU-4825 Xor Sum,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- 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(字典树求异或最大值)
题目链接:点此 我的github地址:点此 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整 ...
- HDU 4825 Xor Sum(字典树)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...
- hdu4825 字典树 XOR
用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...
- HDU--5269 ZYB loves Xor I (字典树)
题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制 我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...
- HDU 5715 XOR 游戏 二分+字典树
XOR 游戏 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5715 Description 众所周知,度度熊喜欢XOR运算(XOR百科). 今天,它 ...
- HDU5715 XOR 游戏 二分+字典树+dp
当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...
随机推荐
- 总结MySQL修改最大连接数的两个方式
最大连接数是可以通过mysql进行修改的,mysql数据库修改最大连接数常用有两种方法,今天我们分析一下这两种方法之间的特点和区别,以便我们能更好的去维护mysql.下面我们来看一下mysql修改最大 ...
- vim出现自动补全的问题
当使用vim编辑文件自动补全文件名称的时候,可能会出现_arguments:451: _vim_files: function definition file not found的错误,这个时候一般都 ...
- mysql 时间格式化参数表笔记
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法: DATE_FORMAT(date,format) 实例: DATE_FORMAT(NOW(),'%b %d %Y %h:% ...
- 事件ID:7026(“下列引导或系统启动驱动程序无法加载: cdrom”)的解决方法
电脑没有安装光驱,而是使用USB光驱/虚拟光驱软件,每次开机后"事件查看器"都显示错误:"下列引导或系统启动驱动程序无法加载: cdrom"(事件ID:7 ...
- JS 把数组按倒序排列
var achearr=[1,2,3,4]; var bchearr=[]; //方法1: for (var i = 0; i <=achearr.length;i++) { bchearr.p ...
- java 基础 --概念--005
1,面向对象特征 封装(encapsulation):隐藏对象的属相和访问细节,仅提供对外公共访问方式.继承(inheritance).多态(polymorphism) 2,类:一组相关的属性和行为的 ...
- get方式传递的数组
- 【uoj#280】[UTR #2]题目难度提升 对顶堆+STL-set
题目描述 给出 $n$ 个数 $a_1,a_2,...,a_n$ ,将其排为序列 $\{p_i\}$ ,满足 $\{前\ i\ 个数的中位数\}$ 单调不降.求字典序最大的 $\{p_i\}$ . 其 ...
- C++模式学习------代理模式
Proxy代理模式 : 为其他对象提供一种代理以控制对这个对象的访问.代理类作为桥梁是请求方和执行方的中间者,将请求方和真正的执行方分割开来,也是两者之间调用的协调者.例如执行类也就是被代理类,可以在 ...
- HDU 6184 Counting Stars
Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!S ...