Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
 
Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T <
10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus
的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
 
Output
对于每组数据,首先需要输出单独一行”Case
#?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
 
Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
 
Sample Output
Case #1:
4
3
Case #2:
4

题解:

今天本来想写一个可持久化Trie树,发现这道题一直没做就补上了。

其实思路很简单,假如说两个数,和同一个数异或,很显然,由于进制,高位上的一个1可以大于低位上所有1,所以即使后面的情况再糟糕,也比取后面好的值高(其实就是1000比0111大)

所以可以建一个01线段树,从高往低插入一个数,比较时取反即可^_^

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,t;
struct pnt{
int child[];
int end;
void res()
{
memset(child,,sizeof(child));
end=;
}
};
struct Trie{
pnt tr[];
int siz;
void dst()
{
siz=;
tr[].res();
}
void insert(int x,int l)
{
int root=;
for(int i=;i>=;i--)
{
int tmp=(bool)((x&(<<i))!=);
if(!tr[root].child[tmp])
{
tr[root].child[tmp]=++siz;
tr[siz].res();
}
root=tr[root].child[tmp];
}
tr[root].end=l;
}
int find(int x)
{
int root=;
for(int i=;i>=;i--)
{
int tmp=(bool)((x&(<<i))!=);
tmp=tmp^;
if(tr[root].child[tmp])
root=tr[root].child[tmp];
else
root=tr[root].child[^tmp];
}
return tr[root].end;
}
}trie;
int a[];
int main()
{
scanf("%d",&t);
for(int ccc=;ccc<=t;ccc++)
{
printf("Case #%d:\n",ccc);
scanf("%d%d",&n,&m);
trie.dst();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
trie.insert(a[i],i);
}
for(int i=;i<=m;i++)
{
int ask;
scanf("%d",&ask);
printf("%d\n",a[trie.find(ask)]);
}
}
return ;
}

HDU4825 Xor Sum(贪心+Trie树)的更多相关文章

  1. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  2. HDU-4825 Xor Sum,字典树好题!

    Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...

  3. HDU4825 Xor Sum(字典树解决最大异或问题)

    Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...

  4. HDU-4825 Xor Sum(字典树求异或最大值)

    题目链接:点此 我的github地址:点此 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整 ...

  5. HDU4825 Xor Sum —— Trie树

    题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  6. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  7. CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  8. Xor Sum 01字典树 hdu4825

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  9. UVALive 4682 XOR Sum (trie)

    题意:求一段连续的数字使得它们的异或和最大. 思路:首先利用前缀和求sum[i],这样求某段连续数字异或和最大就是求某两个j和i满足sum[i]^sum[j-1]最大,问题就变成了找两个数的异或最大. ...

随机推荐

  1. UVA 11991 Easy Problem from Rujia Liu?【STL】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...

  2. 最全Pycharm教程(10)——Pycharm调试器总篇

    最全Pycharm教程(1)--定制外观 最全Pycharm教程(2)--代码风格 最全Pycharm教程(3)--代码的调试.执行 最全Pycharm教程(4)--有关Python解释器的相关配置 ...

  3. Exception: Operation xx of contract xx specifies multiple request body parameters to be serialized without any wrapper elements.

    Operation 'CreateProductCodeStock' of contract 'IChileService' specifies multiple request body param ...

  4. Sql Server通用分页存储过程

    Sql Server2005通用分页存储过程 CREATE PROCEDURE [dbo].[Common_GetPagedList] ( @TableName nvarchar(100), --表名 ...

  5. Python正则表达式初识(二)

    前几天给大家分享了Python正则表达式初识(一),介绍了正则表达式中的三个特殊字符“^”.“.”和“*”,感兴趣的伙伴可以戳进去看看,今天小编继续给大家分享Python正则表达式相关特殊字符知识点. ...

  6. Centos7部署phpMyAdmin系统

    phpMyAdmin是一个使用PHP语言编写,用来管理MYSQL数据库的Web应用系统 一:安装phpMyadmin 下载phpMyadmin最新版本4.8 wget https://files.ph ...

  7. IDEA 热启动,每次更改代码后不用重启服务

    1.ctrl+Shift+Alt+/,选择Registry 2.勾选 compiler.automake.allow.when.app.running(可能不按首字母排序,可以多找找) 3.Setti ...

  8. Linux学习总结(7)——阿里云centeros服务器上安装 jdk,tomcat,mysql

    查看服务器的系统版本 # cat /etc/issue 查看服务器是64位还是32位 #uname -a      或者用:#getconf LONG_BIT 查看当前有没有安装jdk #rpm -q ...

  9. github连接报"ssh: connect to host github.com port 22: Connection timed out"错误

    1. 异常 在连接github时,执行"ssh -T git@github.com" 命令时,出现 ssh: connect to host github.com port 22: ...

  10. 自考之SDT

    软件开发工具(Soft Development Tools)是一本让程序猿了解自己自己所使用工具的书,作为一个刚刚接触编程的小菜鸟.计划工具.分析工具.设计工具.尽管用的都不是非常多,但也有一个概念了 ...