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. (转)windows下编译最新的x264

    二:<windows下编译最新的x264> X264更新的比较快,每天都有更新,但算法模块,基本结构是没有多大变化的.x264都是用C语言写的包括C99,但C99语法是在VC中是没法用的( ...

  2. (转)loff_t *ppos是什么东东

    ssize_t generic_file_read(struct file * filp, char * buf, size_t count, loff_t *ppos) 这是一个文件读函数 我们很容 ...

  3. ubuntu环境JDK安装(转至 http://hi.baidu.com/leo_lovato/item/31d1150d31a06d8002ce1bec)

    ubuntu安装jdk 1.首先去官网http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载最新版的jdk.我下载了 ...

  4. 【Android实战】Android中处理崩溃异常

    public class MainActivity extends ActionBarActivity { public CrashApplication application; @Override ...

  5. 【c语言】推断一个数是奇偶数

    // 推断一个数是奇偶数 #include <stdio.h> void judge_sd(int a) { if ((a & 1) == 0) { printf("是偶 ...

  6. CleanMyMac 4破解版-最强中文版_破解版_激活码_注册码

    最新版CleanMyMac 4中文版本已经发布了,也受到了广大用户的喜爱.众所周知, 注册码是开启软件的钥匙,在获取软件安装包之后需要有效的注册码才能激活软件.但是关于CleanMyMac 4注册码的 ...

  7. JavaSE集合(八)之Map

    前面给大家介绍了集合家族中的Collection家族,这一篇给大家分享的是集合中的另一个家族就是Map家族.以前的时候学习Map的时候没有很认真的去学习,我觉得很多东西还是不是很清楚. 这次我将总结的 ...

  8. 第三章 SqlSessionFactoryBean(MyBatis)

    SqlSessionFactoryBean 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中 ...

  9. Unity UI大小动态设置(Resize Unity UI RectTransform)

    我们在开发过程中发现,要调整Unity UI元素的大小,RectTransform提供了sizeDelta属性可以用来动态修改RectTransform的大小,但同时我们也google到另外一个修改R ...

  10. 静态变量数组实现LRU算法

    LRU算法的解释详情请见 https://baike.baidu.com/item/LRU/1269842 这里百度百科给出的比较详细,然后后面有一个例子 说 LRU(least recently u ...