1602 The XOR Largest Pair 0x10「基本数据结构」例题

描述

在给定的N个整数A1,A2……AN中选出两个进行xor运算,得到的结果最大是多少?

输入格式

第一行一个整数N,第二行N个整数A1~AN。

输出格式

一个整数表示答案。

样例输入

3
1 2 3

样例输出

3

数据范围与约定

  • 对于100%的数据: N<=10^5, 0<=Ai<2^31。

思路:

把一个整数看作长度是32的二进制01串。

要找到异或最大的一对,就是要沿着与当前位相反的字符指针往下访问。如果没有相反的就取相同的。

每加入一个数,就查询一次。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e6 + ;
int n, tot = ;
int trie[maxn * + ][], ed[maxn]; void insertt(int x)
{
/*int str[34];
int len = 0;
while(len < 33){
str[len++] = x & 1;
x >>= 1;
}*/
int p = ;
for(int i = ; i >= ; i--){
int ch = x >> i & ;
if(trie[p][ch] == ){
trie[p][ch] = ++tot;
}
p = trie[p][ch];
}
//ed[p] = true;
} int searchh(int x)
{
/*int len = 0;
int str[34];
while(len < 33){
str[len++] = x & 1;
x >>= 1;
}*/
int p = , ans = ;
for(int i = ; i >= ; i--){
int ch = x >> i & ;
if(trie[p][ch ^ ]){
p = trie[p][ch ^ ];
ans |= << i;
}
else{
p = trie[p][ch];
}
}
return ans;
} int main()
{
scanf("%d", &n);
int ans = ;
for(int i = ; i < n; i++){
int x;
scanf("%d", &x);
insertt(x);
ans = max(ans, searchh(x));
}
printf("%d\n", ans);
return ;
}

CH1602 The XOR Largest Pair【Trie树】的更多相关文章

  1. The XOR Largest Pair(tire树)

    题目 The XOR Largest Pair 解析 一年前听学长讲这道题,什么01trie,好高级啊,所以没学,现在一看.... 看到xor就应该想到二进制,一看数据\(A_i< 2^{31} ...

  2. The XOR Largest Pair [Trie]

    描述 在给定的N个整数A1,A2--AN中选出两个进行xor运算,得到的结果最大是多少? 输入格式 第一行一个整数N,第二行N个整数A1-AN. 输出格式 一个整数表示答案. 样例输入 3 1 2 3 ...

  3. CH 1602 - The XOR Largest Pair - [字典树变形]

    题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...

  4. 「LOJ#10050」「一本通 2.3 例 2」The XOR Largest Pair (Trie

    题目描述 在给定的 $N$ 个整数 $A_1,A_2,A_3...A_n$ 中选出两个进行异或运算,得到的结果最大是多少? 输入格式 第一行一个整数$N$. 第二行$N$个整数$A_i$. 输出格式 ...

  5. The XOR Largest Pair (trie树)

    题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1​,A2​,--,AN​ 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...

  6. The XOR Largest Pair

    刷刷书上的例题 在给定的N个整数A1,A2……An中选出两个进行XOR运算,得到的结果最大是多少?N<=105,0<=Ai<231 SOlution: 我们思考到对于两个数相异或,是 ...

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

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

  8. LOJ10050 The XOR Largest Pair

    题意 在给定的 \(N\) 个整数 \(A_1,A_2,-,A_N\) 中选出两个进行异或运算,得到的结果最大是多少? 对于 \(100\%\) 的数据,\(1\le N\le 10^5, 0\le ...

  9. HDU4825 Xor Sum(贪心+Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

随机推荐

  1. 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope

    解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...

  2. 关于lockkeyword

    线程同步对象keywordlock,是以lock相应的object对象为准,即随意线程lock住lockObj对象之后,其它线程不管在其它不论什么代码段使用lock(lockObj)去进行线程同步时, ...

  3. sql字符串的拼接 (字符串和二进制,erlang的mysql驱动)

    1> list_to_binary(["select * from aa limit","1",",","97"] ...

  4. Why provision Bare Metal

    Here are a few use-cases for bare metal (physical server) provisioning in cloud; there are doubtless ...

  5. Linux(Ubuntu)下搭建ASP.NET Core环境

    今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neu ...

  6. mysql 启动报错--发现系统错误2,系统找不到指定的文件。

    解决方法: 控制面板--找到mysql程序--修复

  7. linux环境下,清空history中记录的历史命令

    需求描述: 今天在数据库主机上操作,通过history看到有刚操作过的历史记录,想把这个清除了, 但是,还不影响后续的命令记录,所以查了下,在此记录. 操作过程: 1.通过history -c命令,完 ...

  8. easyui------dialog如何固定位置

    转载: http://blog.csdn.net/dhdhdh0920/article/details/7438272 代var default_left; var default_top; $('# ...

  9. 打地鼠游戏iOS源代码项目

    打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码.这也是一款高质量的打地鼠游戏源代码.能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的 ...

  10. 在linux中添加环境变量

    首先用命令查看配置了哪些环境变量 env命令查看 编辑环境变量命令  vim ~/.bash_profile Counter_HOME=/home/test/Counterexport Counter ...