CH1602 The XOR Largest Pair【Trie树】
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树】的更多相关文章
- The XOR Largest Pair(tire树)
题目 The XOR Largest Pair 解析 一年前听学长讲这道题,什么01trie,好高级啊,所以没学,现在一看.... 看到xor就应该想到二进制,一看数据\(A_i< 2^{31} ...
- The XOR Largest Pair [Trie]
描述 在给定的N个整数A1,A2--AN中选出两个进行xor运算,得到的结果最大是多少? 输入格式 第一行一个整数N,第二行N个整数A1-AN. 输出格式 一个整数表示答案. 样例输入 3 1 2 3 ...
- CH 1602 - The XOR Largest Pair - [字典树变形]
题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...
- 「LOJ#10050」「一本通 2.3 例 2」The XOR Largest Pair (Trie
题目描述 在给定的 $N$ 个整数 $A_1,A_2,A_3...A_n$ 中选出两个进行异或运算,得到的结果最大是多少? 输入格式 第一行一个整数$N$. 第二行$N$个整数$A_i$. 输出格式 ...
- The XOR Largest Pair (trie树)
题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1,A2,--,AN 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...
- The XOR Largest Pair
刷刷书上的例题 在给定的N个整数A1,A2……An中选出两个进行XOR运算,得到的结果最大是多少?N<=105,0<=Ai<231 SOlution: 我们思考到对于两个数相异或,是 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- LOJ10050 The XOR Largest Pair
题意 在给定的 \(N\) 个整数 \(A_1,A_2,-,A_N\) 中选出两个进行异或运算,得到的结果最大是多少? 对于 \(100\%\) 的数据,\(1\le N\le 10^5, 0\le ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
随机推荐
- window,centos双系统坏了
在centos中格式化SD卡的时候,操作错误,误将windows系统C盘的给格式化了.C盘是ntf格式的,现在却变为了fat32的格式. 重启系统,发现还是可以进入到centos,但是window进入 ...
- alien 进行rpm 包和deb 包之间的转换
今天安装一个pandoc, 官方只提供了一个deb 的二进制包,为了在redhat 上安装,需要将deb 包转换成rpm 包. 使用工具alien : http://ftp.de.debian.org ...
- 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 ...
- Mysql利用match...against进行全文检索
在电商项目中,最核心的功能之一就是搜索功能,搜索做的好,整个电商平台就是个优秀的平台.一般搜索功能都使用搜索引擎如Lucene.solr.elasticsearch等,虽然这功能比较强大,但是对于一些 ...
- WPF 自定义命令 以及 命令的启用与禁用
自定义命令: 在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...
- Python 爬虫知识点 - XPath
http://cuiqingcai.com/2621.html 一.基础介绍 <bookstore> <book> <title>Harry Potter</ ...
- 安装php5.5 mssql扩展报错
./configure 后,直接make可能会出现libtool: link: `php_mssql.lo' is not a valid libtool object 的错误. make clean ...
- Linux环境下$开头的相关变量的含义
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- ios错误大全
/读取数据库数据 -(void)movethesqlitefile{ NSString *sourcepath=[[NSBundle mainBundle]pathForResource:@" ...
- Runtime 运行时之一:消息传递
什么是Runtime? Runtime顾名思义即为运行时.就是系统运行时候的一些机制,它提供了一些使得对象之间能够传递消息的重要函数,其中最主要的就是消息机制了.相较于C语言而言,C语言使用的是“静态 ...