The XOR Largest Pair(Tire字典树应用)
题目链接:传送门
思路:建立一个32位的字典树,对每一个要插入的数字查找它异或的最大值(就是尽量全部二进制的值都相反),
然后获得两个数异或的最大值。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5+;
int ch[maxn*][],tot;
void Insert(int x)
{
int i,u=,c;
for(i=;i>=;i--)
{
c=(x>>i)&;
if(!ch[u][c]) ch[u][c]=++tot;
u=ch[u][c];
}
}
int search(int x)
{
int i,u=,c,o,ans=;
for(i=;i>=;i--)
{
c=(x>>i)&;
o=c^;
if(ch[u][o]) u=ch[u][o],ans=ans<<|;
else u=ch[u][c],ans=ans<<;
}
return ans;
}
int MAX(int x,int y)
{
return x>y?x:y;
}
int main(void)
{
int n,i,x,ans;
while(~scanf("%d",&n))
{
ans=;
memset(ch,,sizeof(ch));
tot=;
for(i=;i<n;i++)
{
scanf("%d",&x);
ans=MAX(ans,search(x));
Insert(x);
}
printf("%d\n",ans);
}
return ;
}
The XOR Largest Pair(Tire字典树应用)的更多相关文章
- 题解0014:信奥一本通1472——The XOR Largest Pair(字典树)
题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1472 题目描述:在给定的 N 个整数中选出两个进行异或运算,求得到的结果最大是多少. 看到这 ...
- The XOR Largest Pair(字典树)
题目描述 在给定的 N 个整数 A1,A2,-,AN 中选出两个进行异或运算,得到的结果最大是多少? 输入格式 第一行一个整数 N. 第二行 N 个整数 Ai. 输出格式 一个整数表示答案. 样例 ...
- The XOR Largest Pair(tire树)
题目 The XOR Largest Pair 解析 一年前听学长讲这道题,什么01trie,好高级啊,所以没学,现在一看.... 看到xor就应该想到二进制,一看数据\(A_i< 2^{31} ...
- CH1602 The XOR Largest Pair【Trie树】
1602 The XOR Largest Pair 0x10「基本数据结构」例题 描述 在给定的N个整数A1,A2……AN中选出两个进行xor运算,得到的结果最大是多少? 输入格式 第一行一个整数N, ...
- The XOR Largest Pair (trie树)
题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1,A2,--,AN 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...
- 「LOJ#10050」「一本通 2.3 例 2」The XOR Largest Pair (Trie
题目描述 在给定的 $N$ 个整数 $A_1,A_2,A_3...A_n$ 中选出两个进行异或运算,得到的结果最大是多少? 输入格式 第一行一个整数$N$. 第二行$N$个整数$A_i$. 输出格式 ...
- CH 1602 - The XOR Largest Pair - [字典树变形]
题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...
- 019(The XOR Largest Pair)(字典树)
题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1472 题目思路:异或是啥呀? 异或就是把两个数字变成位数相同的二进制在同位比较,相同为0,不同为 ...
- 字典树-THE XOR largest pair
题目:给你n个数字A1,A2....An ,问从中选出两个数字异或运算得到的最大结果是多少 0<=Ai<231 用字典树,记录每个数字的31位2进制01串(int 为4个字节,每个字节8个 ...
随机推荐
- TZOJ 2546 Electricity(去掉割点后形成的最大连通图数)
描述 Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The compa ...
- sql查询 !='' 和 is not null的区别
select * from table where a is not null 会把有内容的和内容为空的都查出来而select * from table where a != '' 只会把有内容的查出 ...
- 项目打包 TestFlight用法
TestFlight用法 包教包会(iOS APP官方测试工具) https://www.jianshu.com/p/4be185e4069c
- MVC是架构模式,而不是设计模式
最早学编程的时候看过一些书,印象深刻的一本书<设计模式解析>,那本书给我后来的工作提供了很大的帮助. 他叫我站在问题模型的立场上指定解决方法,也教会了我软件设计中每个问题都可以细化到到不可 ...
- java 知识汇总
一.springboot cloud 1.maven 配置 parent:org.springframework.boot:sping-boot-starter-parent dependencies ...
- PDO 代码
<?php try{ $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root&qu ...
- IDEA工具 常用的设置
以下均为笔者常用的的设置. 首先选择菜单栏的选项:“File” ----> “settings” 1.字体设置 (1)代码区的字体设置 如下图的选择,"Primary font&quo ...
- Android.Tools.Eclipse hangs at the Android SDK Content Loader
Eclipse hangs at the Android SDK Content Loader http://stackoverflow.com/questions/13489141/eclipse- ...
- 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur
原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...
- mysql 压缩方法
show global variables like 'innodb_file_format%';alter table t row_format=COMPRESSED;