B - Dr. Evil Underscores

Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2,…,an and challenged him to choose an integer XX such that the value max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X) is minimum possible, where ⊕⊕ denotes the bitwise XOR operation.

As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).

Input

The first line contains integer nn (1≤n≤1051≤n≤105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1).

Output

Print one integer — the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).

Examples

Input
3
1 2 3
Output
2
Input
2
1 5
Output
4

Note

In the first sample, we can choose X=3X=3.

In the second sample, we can choose X=5X=5.

  题目大意:在给出的n个整数中选出一个整数,使得剩下所有数与这个数的得到的最大异或结果最小

  

 #include<iostream>
#include<algorithm>
#include<vector>
using namespace std; vector<int> v;
const int N = 1e5 + ;
int n, x, maxn, cnt; int DFS(int cnt, vector<int> v){
if(v.size()== || cnt<) return ;//数组中没有整数或二进制有负数位自然不用再考虑
vector<int> v1, v0;//注意这是在函数中定义的局部的不定长数组
for(int i=; i<v.size(); i++){
//判断某数的二进制的某一位上,是1即插入v1,是0即插入v0
if((v[i]>>cnt) & ) v1.push_back(v[i]);
else v0.push_back(v[i]);
}
//若所有整数的二进制在这一位上均为同一个数(不管是0还是1)都可以用0或1使得其异或结果均为0,从而达到使异或结果最小的目的
if(v1.empty()) return DFS(cnt-, v0);
else if(v0.empty()) return DFS(cnt-, v1);
//如果所有整数的二进制在这一位上不可避免的既有1有有0,则其异或结果可以使1也可以是0,而结果是取最大的异或结果,即1
else return min(DFS(cnt-, v1), DFS(cnt-, v0)) + (<<cnt);
} int main(){
scanf("%d", &n);
for(int i=; i<n; i++){
scanf("%d", &x);
v.push_back(x);
if(x > maxn) maxn = x;
}
//找到其中最大的数,并统计出其二进制有几位
while(maxn){
maxn >>= ;
cnt ++;
}
printf("%d",DFS(cnt, v));
return ;
}

(1)即使题目放在搜索训练中,我也想不到和搜索有什么关系。其实如果用最简单暴力的方法去做,就是无脑遍历呗,必然超时的原始人操作,这里所用的方法,就是函数递归,递归到头,省时省力。

(2)因为题目中的操作是异或,所有需要将所给的整数,进行二进制处理,而思路也注释在了代码中:如果所有的整数的二进制形式,在某一位上均位同一个数,则很轻易地能用另一个数,使这一位上的异或结果均为0;相反的,如果所有整数在这一位上的数字既有1又有0,也就是不管怎么样,异或结果都会碰到1,题目找到是最大异或结果的最小值,所以当碰到这种结果时只能乖乖取1。

(3)DFS的递归。从最大整数的最高位开始,一位一位地向后递归,在每一位上都得到最理想的异或结果,合起来,就是所求的最大异或结果的最小值。

DFS-B - Dr. Evil Underscores的更多相关文章

  1. CF1285 --- Dr. Evil Underscores

    CF1285 --- Dr. Evil Underscores 题干 Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_ ...

  2. CF1285D Dr. Evil Underscores

    挂个链接 Description: 给你 \(n\) 个数 \(a_1,a_2,--,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小 ...

  3. C - Dr. Evil Underscores CodeForces - 1285D 二进制

    题目大意:n个数,任意整数x对这n个数取异或值,然后使最大值最小. 思路:数据范围最大为pow(2,30);所以考虑二进制的话,最多有30位.对于某一位d,然后考虑数组v中每一个元素的d为是0还是1, ...

  4. codeforces 1285D. Dr. Evil Underscores(字典树)

    链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...

  5. codeforces每日一题1-10

    目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...

  6. Codeforces Round #613 (Div. 2) A-E简要题解

    contest链接:https://codeforces.com/contest/1285 A. Mezo Playing Zoma 签到 #include<iostream> #incl ...

  7. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  8. 图--DFS求连通块

                                  The GeoSurvComp geologic survey company is responsible for detecting u ...

  9. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

随机推荐

  1. 持续集成:jenkins集合

    持续集成:jenkins集合 jenkins(一):   持续集成和Jenkins简介 jenkins(二):   Jenkins的安装 jenkins(三):   Jenkins的应用场景和job ...

  2. k8s系列---stateful(有状态应用副本集)控制器

    http://blog.itpub.net/28916011/viewspace-2215046/ 在应用程序中,可以分为有状态应用和无状态应用. 无状态的应用更关注于群体,任何一个成员都可以被取代. ...

  3. maven mvn 安装介绍

    maven是什么? Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具 Maven 除了以程 ...

  4. pip 安装源-Python学习

    1.国内常用的安装源 -- 豆瓣:https://pypi.douban.com/simple -- 阿里:https://mirrors.aliyun.com/pypi/simple --中国科技大 ...

  5. Linux运维--12.手动部署Rabbit集群

    1.安装rabbit组件 10.100.2.51 controller1 10.100.2.52 controller2 10.100.2.53 controller3 #每个节点 yum insta ...

  6. StackExchange.Redis 之 Set集合 类型示例

    话不多说直接上代码: // set添加单个元素 stopwatch.Start(); "); stopwatch.Stop(); Console.WriteLine("set添加单 ...

  7. 面型对象和UML类图

    面向对象 why? 1.程序执行:顺序,判断,循环,----结构化 2.面向对象----数据结构化 3.面向计算机,结构化的才是最简单的 4.变成应该 简单&抽象 一个基本的类 class P ...

  8. jQuery on 绑定的事件 执行两次

    $(".class1").on("click",".class2",function(){ alert('提示'); }); 上面代码,怎么 ...

  9. robotframework安装与详解

    Robot Framework(以下简称rf)是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行.主要用于轮次 ...

  10. 常见的MIME类型与00截断

    常见的MIME类型 1)超文本标记语言.html文件的MIME类型为:text/html 2)普通文本.txt文件的MIME类型为:text/plain 3)PDF文档.pdf的MIME类型为:app ...