Description

给定一些数,求这些数中两个数的异或值最大的那个值

Input

多组数据。第一行为数字个数n,1 <= n <= 10 ^ 5。接下来n行每行一个32位有符号非负整数。

Output

任意两数最大异或值

Sample Input

3

3

7

9

Sample Output

14

Hint

Source

CSGrandeur的数据结构习题

异或

异或运算符(^ 也叫xor(以后做题会遇到xor,就是异或))

规则:0^0 = 0,01=1,10=1,1^1=0 参加位运算的两位只要相同为0,不同为1

例子:3^5 = 6(00000011^00000101=00000110)

暴力(不用说)

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxN = 1e5 + 7; int a[maxN]; int main() {
int n;
scanf("%d",&n);
for(int i = 1;i <= n;++ i) {
scanf("%d",a[i]);
}
int maxn = 0;
for(int i = 1;i < n;++ i) {
for(int j = i + 1;j <= n;++ j) {
maxn = max(maxn,a[i] xor a[j]);
}
}
printf("%d",maxn);
}

正解:我们可以发现异或的一些性质,不同为1,根据等比数列求和,

等比数列求和

$2^1 + 2^2 + 2^3 + 2^4 < 2^5 $

证明: 设\(2^1 + 2^2 + 2^3 + 2^4\)为S,那么\(2S = 2^2 + 2^3 + 2^4 + 2^5 - S\);

等于$2^5 - 1 < 2^5 $

为什么要涉及到这个呢,因为我们要贪心的选取,一定要看看有没有特殊情况.

我们从高位开始选择与其不同的二进制位.就Ok了.

code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int maxN = 1e5 + 7; int a[maxN];
int son[maxN * 20][3],cnt; void init() {
memset(son,0,sizeof(son));
cnt = 0;
return;
}
void build(ll a) {
int now = 0;
for(int i = 32;i >= 0;-- i) {
int qwq = (a >> i & 1);
if( !son[now][qwq] )son[now][qwq] = ++ cnt;
now = son[now][qwq];
}
} ll find(ll a) {
ll res = 0,now = 0;
for(int i = 32;i >= 0;-- i) {
bool tmp = ((a >> i & 1) ^ 1);
if(son[now][tmp]) now = son[now][tmp],res = res | (1LL << i);
else now = son[now][tmp ^ 1];
}
return res;
} int main() {
int n;
while(~scanf("%d", &n)) {
init();
for(int i = 1;i <= n;++ i)
scanf("%d",&a[i]);
for(int i = 1;i <= n;++ i)
build(a[i]);
ll Ans = 0;
for(int i = 1;i <= n;++ i) {
Ans = max(Ans,find(a[i]));
}
printf("%lld\n",Ans);//一定要注意:换行!!!!
}
}

CSU 1216异或最大值 (0-1 trie树)的更多相关文章

  1. CSU 1216 异或最大值

    求n个数里面,求两两异或的最大值 直接来肯定会超时 既然要异或最大值,那么两个数的二进制肯定是正好错开为好...为了能快速找到错开的数,确实有点难想到,用字典树,按二进制数插入,再一个一个在字典树里面 ...

  2. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  3. 中南oj 1216: 异或最大值 数据结构

    1216: 异或最大值 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 98  Solved: 29 [Submit][Status][Web Boar ...

  4. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  5. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  6. 51nod 1295 XOR key-区间异或最大值-可持久化01Trie树(模板)

    1295 XOR key 2 秒 262,144 KB 160 分 6 级题   给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] ...

  7. 【bzoj3689】异或之 可持久化Trie树+堆

    题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...

  8. [BZOJ4103][Thu Summer Camp 2015]异或运算 可持久化Trie树

    4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec  Memory Limit: 512 MB Description 给定长度为n的数列X={x1 ...

  9. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

随机推荐

  1. Cinder Columns

    http://www.screencast.com/users/xiangxinyong/folders/Smaug http://www.screencast.com/t/SLqCyOwtBRl

  2. how to use Eclipse with Maven

    install Eclipse LUNA; download and unzip Maven; Eclipse=>window=>preference=>maven=>inst ...

  3. React.js 小书 Lesson7 - 组件的 render 方法

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson7 转载请注明出处,保留原文链接和作者信息. React.js 中一切皆组件,用 React. ...

  4. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. checkbox判断选中的三种方法

    方法一: if ($("#checkbox-id")get(0).checked) {     // do something } 方法二: if($('#checkbox-id' ...

  6. JSP中的<%%>,<%! %>,<%= %>,<%-- --%>

    <%  %>             添加java代码 <%!   %>   添加java方法 <%= %>    将变量或表达式输出到页面 <%--   - ...

  7. Python并发编程之进程池与线程池

    一.进程池与线程池 python标准模块concurrent.futures(并发未来) 1.concurrent.futures模块是用来创建并行的任务,提供了更高级别的接口,为了异步执行调用 2. ...

  8. CF1096C Polygon for the Angle

    思路: 要想到正n边形中所有可能的ang为180 * k / n (1 <= k <= n - 2). 根据n = 180 * k / ang, n是大于等于3的整数,并且n >= ...

  9. array_map()关于回调函数的总结

    array_map()函数的第一个参数可以是匿名函数,系统函数,也可以是自己自定义的函!在全局空间下,这些函数的调用都很简单 在这里就不多说了!我们主要讨论的是该函数调用类中的方法和静态方法的区别 摘 ...

  10. jQuery实现焦点图[兼容ie7+]

    HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...