HDU4825 Xor Sum(贪心+Trie树)
输入的第一行是一个整数T(T <
10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus
的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
#?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
题解:
今天本来想写一个可持久化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树)的更多相关文章
- 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,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- HDU-4825 Xor Sum(字典树求异或最大值)
题目链接:点此 我的github地址:点此 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整 ...
- 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这个位置上不相同 ...
- 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 ...
- Xor Sum 01字典树 hdu4825
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- UVALive 4682 XOR Sum (trie)
题意:求一段连续的数字使得它们的异或和最大. 思路:首先利用前缀和求sum[i],这样求某段连续数字异或和最大就是求某两个j和i满足sum[i]^sum[j-1]最大,问题就变成了找两个数的异或最大. ...
随机推荐
- 把握linux内核设计思想(三):下半部机制之软中断
[版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途] 中断处理程序以异步方式执行,其会打断其它重要代码,其执行时该中 ...
- 同一台服务器部署多个WEB应用,SESSION冲突的解决方法
由于一台服务器上使用Tomcat部署多个WEB项目,而项目因为用到框架都是一样的,导致同时运行,session相互冲突,这个登录后,那个就得重新登录,造成了使用不方便,解决办法如下: 在server. ...
- 118.类包装器与lambda函数包装器(伪函数实现)
#include <iostream> #include <list> using namespace std; //函数包装器,左边参数右边函数 template<cl ...
- 一张图了解javaJwt
1.什么是javaJwt? JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims ...
- VS初始化设置
来源于网上整理和 书<aps.net mvc企业级实战>中. 1.vs模版 版权注释信息 1.我的电脑上VS2015安装在D盘中,所以找的目录为:D:\Program Files (x86 ...
- input[type=date]
::-webkit-datetime-edit – 控制编辑区域的 ::-webkit-datetime-edit-fields-wrapper – 控制年月日这个区域的 ::-webkit-date ...
- 原生js模拟jquery中的addClass和removeClass方法
js代码: //添加类 function addClass(obj,className) { if(obj.className == '') { //如果没有class obj.className = ...
- HOJ——T 2430 Counting the algorithms
http://acm.hit.edu.cn/hoj/problem/view?id=2430 Source : mostleg Time limit : 1 sec Memory limit : 64 ...
- Linux中配置网桥
使用kvm虚拟机时,有时候需要自己添加网桥供guest使用. 不使用libvirt来管理的话,可以使用以下方法创建网桥并绑定到物理网卡(RHEL6/Fedora已实验): 1.创建网桥配置文件ifcf ...
- [Python] Slicing Lists
In addition to accessing individual elements from a list we can use Python's slicing notation to acc ...