CSU 1214 找最大异或值
题目大意:
给定一堆数,从中找2个数异或得到的最大值
直接暴力会超时,我们要考虑对于每一个数去匹配找到异或的最大值,我们希望2进制越前面的数尽可能都为1
所以我们用 0-1 字典树保存这些数,因为一个int型的正整数最多2进制到第30位,所以我们用31层高的字典树保存,第一层为root节点
每次查询操作都是对于当前数的2进制位查找,如果与之相反的方向有点,就往与之相反的方向向下找,这样异或才为1,没有,就顺着当前相同方向向下找,那样异或值为0
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; int ans;
struct Node{
int num;
Node *next[];
Node(){
num = ;
next[] = NULL;
next[] = NULL;
}
}; Node *root = new Node(); void add_node(int x)
{
int k = (x&(<<)) == ?:;
Node *q;
if(!root->next[k]){
root->next[k] = new Node();
}
q = root->next[k];
for(int i = ; i >= ; i--){
k = (x&(<<i)) == ?:;
if(!q->next[k])
q->next[k] = new Node();
q = q->next[k];
}
q->num = x;
} void query(int x)
{
int k = (x&(<<)) == ?:;
Node *q;
if(!root->next[k^]){
q = root->next[k];
}
else q = root->next[k^];
for(int i = ; i >= ; i--){
k = (x&(<<i)) == ?:;
if(!q->next[k^])
q = q->next[k];
else
q = q->next[k^];
}
// cout<<" get num : "<<q->num<<endl;
ans = max(ans , q->num ^ x); } int main()
{
//freopen("test.in","rb",stdin);
int n,a;
while(~scanf("%d",&n)){
root = new Node();
ans = ;
for(int i=;i<n;i++){
scanf("%d",&a);
add_node(a);
query(a);
//cout<<" jsd: "<<i<<" "<<ans;
} printf("%d\n",ans);
}
return ;
}
CSU 1214 找最大异或值的更多相关文章
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)
题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...
- Leetcode 421.数组中两数的最大异或值
数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...
- bzoj 2819 Nim dfn序+树状数组维护区间异或值
题目大意 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- HJA的异或值
HJA的异或值 查看 提交 统计 提问 总时间限制: 20000ms 内存限制: 512000kB 描述 形态形成场(Morphogenetic Field)假说是Rupert Sheldrake ...
- CSDN 正整数异或值问题
题目详情: http://student.csdn.net/mcs/programming_challenges?page=4 给你n个正整数,请你计算出有多少对数的异或值小于等于k. 输入描写叙述: ...
- 异或值 xor
题目描述 给出一个 N 个点的带权无向图,要求从 1 号点到 N 号点的一条路径,使得路径上的边 权异或值最大. 输入格式 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M ...
随机推荐
- Android组件化开发(注意事项)
1.Manifest合并 在Android studio编译项目时,无论你使用了几个Module都会把所有Manifest最终合并成一个,需要我们注意的是application标签下这个几个属性引用的 ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射 下一篇:Farseer.net轻量级ORM开源框 ...
- c# winform如何屏蔽键盘上下左右键
重写事件: protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Up || keyData == ...
- jQuery 首页搜索区域模块随页面滑动而变化
/*搜索区块的颜色变化*/ function search(){ var searchBox = document.querySelector('.m_head'); var bannerBox = ...
- Linux Mint 教程
Linux Mint 安装文本编辑软件 sudo apt-get install gedit linux操作系统上面开发程序, 光有了gcc 是不行的它还需要一个 build-essential软 ...
- flutter 上传图片 image_picker 的使用
Github地址: https://github.com/flutter/plugins/tree/master/packages/image_picker packages地址: https://p ...
- Spring Boot . 4 -- 定制 Spring Boot 配置
覆写 Auto-Configuration 的类 利用外部属性进行动态配置 [本文] 定制 Error 页面 [第二篇] Spring Boot的自动配置可以节省很多无趣的配置工作,但是并不是所有的自 ...
- div 可视化区域弹窗居中
效果: css: .div_alt { position: fixed; border-radius: 5px; top: 50%; left: 50%; width: auto; min-width ...
- swiper 旋转木马效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- [Python3网络爬虫开发实战] 7-动态渲染页面爬取
在前一章中,我们了解了Ajax的分析和抓取方式,这其实也是JavaScript动态渲染的页面的一种情形,通过直接分析Ajax,我们仍然可以借助requests或urllib来实现数据爬取. 不过Jav ...