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

描述

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

输入格式

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

输出格式

一个整数表示答案。

样例输入

  1. 3
  2. 1 2 3

样例输出

  1. 3

数据范围与约定

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

思路:

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

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

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

  1. #include <iostream>
  2. #include <set>
  3. #include <cmath>
  4. #include <stdio.h>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <map>
  8. using namespace std;
  9. typedef long long LL;
  10. #define inf 0x7f7f7f7f
  11.  
  12. const int maxn = 1e6 + ;
  13. int n, tot = ;
  14. int trie[maxn * + ][], ed[maxn];
  15.  
  16. void insertt(int x)
  17. {
  18. /*int str[34];
  19. int len = 0;
  20. while(len < 33){
  21. str[len++] = x & 1;
  22. x >>= 1;
  23. }*/
  24. int p = ;
  25. for(int i = ; i >= ; i--){
  26. int ch = x >> i & ;
  27. if(trie[p][ch] == ){
  28. trie[p][ch] = ++tot;
  29. }
  30. p = trie[p][ch];
  31. }
  32. //ed[p] = true;
  33. }
  34.  
  35. int searchh(int x)
  36. {
  37. /*int len = 0;
  38. int str[34];
  39. while(len < 33){
  40. str[len++] = x & 1;
  41. x >>= 1;
  42. }*/
  43. int p = , ans = ;
  44. for(int i = ; i >= ; i--){
  45. int ch = x >> i & ;
  46. if(trie[p][ch ^ ]){
  47. p = trie[p][ch ^ ];
  48. ans |= << i;
  49. }
  50. else{
  51. p = trie[p][ch];
  52. }
  53. }
  54. return ans;
  55. }
  56.  
  57. int main()
  58. {
  59. scanf("%d", &n);
  60. int ans = ;
  61. for(int i = ; i < n; i++){
  62. int x;
  63. scanf("%d", &x);
  64. insertt(x);
  65. ans = max(ans, searchh(x));
  66. }
  67. printf("%d\n", ans);
  68. return ;
  69. }

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. window,centos双系统坏了

    在centos中格式化SD卡的时候,操作错误,误将windows系统C盘的给格式化了.C盘是ntf格式的,现在却变为了fat32的格式. 重启系统,发现还是可以进入到centos,但是window进入 ...

  2. alien 进行rpm 包和deb 包之间的转换

    今天安装一个pandoc, 官方只提供了一个deb 的二进制包,为了在redhat 上安装,需要将deb 包转换成rpm 包. 使用工具alien : http://ftp.de.debian.org ...

  3. chrome 版本升级到56,报错Your connection is not private NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM

    ubuntu14.04 解决方法: $ sudo apt-get install libnss3-1d ref: http://stackoverflow.com/questions/42085055 ...

  4. Mysql利用match...against进行全文检索

    在电商项目中,最核心的功能之一就是搜索功能,搜索做的好,整个电商平台就是个优秀的平台.一般搜索功能都使用搜索引擎如Lucene.solr.elasticsearch等,虽然这功能比较强大,但是对于一些 ...

  5. WPF 自定义命令 以及 命令的启用与禁用

    自定义命令:     在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...

  6. Python 爬虫知识点 - XPath

    http://cuiqingcai.com/2621.html 一.基础介绍 <bookstore> <book> <title>Harry Potter</ ...

  7. 安装php5.5 mssql扩展报错

    ./configure 后,直接make可能会出现libtool: link: `php_mssql.lo' is not a valid libtool object 的错误. make clean ...

  8. Linux环境下$开头的相关变量的含义

    $0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...

  9. ios错误大全

    /读取数据库数据 -(void)movethesqlitefile{ NSString *sourcepath=[[NSBundle mainBundle]pathForResource:@" ...

  10. Runtime 运行时之一:消息传递

    什么是Runtime? Runtime顾名思义即为运行时.就是系统运行时候的一些机制,它提供了一些使得对象之间能够传递消息的重要函数,其中最主要的就是消息机制了.相较于C语言而言,C语言使用的是“静态 ...