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]代表前 ...
随机推荐
- selenium 关键字驱动部分设计思路
1 说明: 1.以下的代码亲测直接可用, 2.设计思路来自博客园的 张飞_ :http://www.cnblogs.com/zhangfei/p/5330994.html / http://w ...
- BeanCopier
cglib是一款比较底层的操作java字节码的框架. 下面通过拷贝bean对象来测试BeanCopier的特性: public class OrderEntity { private int id; ...
- 【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案
开篇总结:其实目前无法解决这个bug. 这两天做项目遇到了这个case,项目需求是打开页面的时候,input元素自动弹起键盘.由于各种方面的考虑,我们希望通过setTimeout延时200毫秒让inp ...
- CF271D_Good Substrings
给一个原串,以及那些字符是坏的,现在问你可以从原串中取出多少个不同子串,使得其所含的坏字符的个数不超过一个定数. 这个题目网上有各种各样的解法.如hash,tire. 我说一下我的解法. 解法一:后缀 ...
- vyos 基础配置
vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...
- STL 算法介绍
STL 算法介绍 算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm ...
- PHP中文字符gbk编码与UTF-8编码的转换
通常PHP中上传文件,如果文件名称有中文字符,上传之后的名称是无法写入到本地的,因为上传来的编码格式一般是UTF-8的格式,这种格式是无法给文件命名并且存储到操作系统磁盘.在写入之前需要将其转换为gb ...
- AJAX实现无刷新登录
最近学习了如何实现无刷新登录,大体的效果如下(界面比较丑,请自行忽略....): 点击登录按钮时弹出登录窗口,输入正确的用户名密码后点击登录则登录窗口关闭,状态改为当前用户名. 第一步: 首先弹出窗口 ...
- Android Support WorkManager使用详解
使用WorkManager调度任务 WorkManager是一个库, 用以将工作入队, 当该工作的约束条件得到满足之后, WorkManager保证它的执行. WorkManager允许观测工作的状态 ...
- 洛谷 P4091 [HEOI2016/TJOI2016]求和 解题报告
P4091 [HEOI2016/TJOI2016]求和 题目描述 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: \[ f(n)=\sum_{i=0}^n\ ...