HDU4825:Xor Sum——题解
http://acm.hdu.edu.cn/showproblem.php?pid=4825
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
板子题。
看到异或想到把数拆成二进制,从高位到低位存在Trie树中。
然后对于每次询问的s同样拆成二进制,在Trie树上跑,显然对于高位越是1越好,所以我们尽可能让这位的两个数不一样。
换句话说s的第i位要和k的第i位尽可能不同(当然如果没有选择也只能走相同的了)
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int N=1e5+;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int son[],id;
}tr[*N];
int tot,a[N];
void insert(int x,int id){
int now=;
for(int i=;i>=;i--){
bool l=x&(<<i);
if(!tr[now].son[l])tr[now].son[l]=++tot;
now=tr[now].son[l];
}
tr[now].id=id;
return;
}
inline void init(){
memset(tr,,sizeof(tr));
tot=;
}
int main(){
int t=read();
for(int test=;test<=t;test++){
printf("Case #%d:\n",test);
init();
int n=read(),m=read();
for(int i=;i<=n;i++)insert(a[i]=read(),i);
for(int i=;i<=m;i++){
int x=read(),now=;
for(int j=;j>=;j--){
bool op=x&(<<j);
if(tr[now].son[op^])now=tr[now].son[op^];
else now=tr[now].son[op];
}
printf("%d\n",a[tr[now].id]);
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
HDU4825:Xor Sum——题解的更多相关文章
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- HDU-4825 Xor Sum,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- HDU4825 Xor Sum
题意 给定一个集合后, 求一组查询中每个数和集合中任一元素异或的最大值. 题解 异或的规律是这样的 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 而最大值即是 ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- HDU4825:Xor Sum 解题报告(0/1 Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数. 随后 Prometheus 将向 Ze ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- ☆ [HDU4825] Xor Sum「最大异或和(Trie树)」
传送门:>Here< 题意:给出一个集合,包含N个数,每次询问给出一个数x,问x与集合中的一个数y异或得到最大值时,y是多少? 解题思路 由于N,M非常大,暴力显然不行.抓住重点是异或,所 ...
- HDU4825 Xor Sum (01Trie)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
随机推荐
- redux devtools调试工具
项目安装: npm install redux-devtools-extension -dev 谷歌搜索 Redux DevTools 安装: 使用: 主要用到state&Dispatcher ...
- Java: Replace a string from multiple replaced strings to multiple substitutes
Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...
- Qt 3D Studio 1.0 Resleased
Qt 这家伙又整出一个新东西了,Qt 3D Studio 1.0 新闻链接:https://blog.qt.io/blog/2017/11/30/qt-3d-studio-1-0-released/ ...
- 在Android上运用Anko和Kotlin开发数据库:SQLite从来不是一件轻松的事(KAD25)
作者:Antonio Leiva 时间:Mar 30, 2017 原文链接:https://antonioleiva.com/databases-anko-kotlin/ 事实告诉我们:在Androi ...
- Python 发邮件例子
Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...
- 怎样用Eclipse将Java源代码生成可执行文件[转]
eclipse将java源代码生成jar可执行文件 用eclipse做了一个web项目的自动化测试,自己用的时候倒是很方便,打开eclipse直接运行即可,但是分享给其他小伙伴用的时候就不太方便,希望 ...
- Objective-C 点语法 成员变量的作用域 @property和@synthesize关键字 id类型
点语法 1.利用点语法替换set方法和get方法 方法调用 Student *stu = [Student new]; [stu setAge : 18]; int age = [stu age]; ...
- Grid 网格布局
CSS 网格布局(Grid Layout) 是CSS中最强大的布局系统. 这是一个二维系统,这意味着它可以同时处理列和行,不像 flexbox 那样主要是一维系统. 你可以通过将CSS规则应用于父元素 ...
- 安卓客户端浏览器ajax注意
这两天被一个bug搞疯了,就是公司安卓app上我负责的网页死活不进ajax,一开始我用的是post方式提交的,但是参数那一栏没写,直接把参数写在url上了,后来老大跟我说post不写参数会出问题,后来 ...
- LeetCode 109——有序链表转化二叉搜索树
1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入 ...