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 ...
随机推荐
- 与其他Javascript类库冲突解决方案
$(document).ready(function() { var $jq = jQuery.noConflict(); $jq('#id').show(); });
- 【Mongo】聚合函数
http://blog.csdn.net/miyatang/article/details/20997313 SQL Terms, Functions, and Concepts MongoDB Ag ...
- Tomcat源码学习
Tomcat源码学习(一) 转自:http://carllgc.blog.ccidnet.com/blog-htm-do-showone-uid-4092-type-blog-itemid-26309 ...
- 查询_修改SQL Server 2005中数据库文件存放路径
1.查看当前的存放路径: select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.maste ...
- VC++ :实现简单的文件拖放(Drag and Drop)功能
1) VC++ 6.0 新建一个基于对话框的MFC的工程,取名MfcDropFiles: 2) 去除默认的控件,包括确定/取消按钮,以及一个静态文本: 3) 在对话框空白区域拖放一个ListBox控件 ...
- Navicat for Mysql 如何备份数据库
Navicat for Mysql 如何备份数据库 打开界面如下 打开自己的的数据库 点击需要备份的数据库名 未完!!! 文章来自:http://jingyan.baidu.com/article/f ...
- 非常不错的前端框架Kendo-ui
近期公司在做重构,准备换前端框架由Extjs换kendo-ui,问什么要换框架呢?主要有以下几个原因: Extjs太重,偏向后端语言,前端写起来费劲 Extjs执行太慢(这是主要原因),因为Extjs ...
- Java web url 规范
设计URI应该遵循的原则 URI是网站UI的一部分,因此,可用的网站应该满足这些URL要求 简单,好记的域名 简短(short)的URI 容易录入的URI URI能反应站点的结构 URI是可以被用户猜 ...
- js二级联动
<body> <section> <a>省份</a> <select id="province"> <option ...
- 关于cstring ->string-> const char * 用U2A一步转换 错误的内存问题
// CStringTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #incl ...