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. Tensorflow和pytorch安装(windows安装)

    一. Tensorflow安装 1. Tensorflow介绍 Tensorflow是广泛使用的实现机器学习以及其它涉及大量数学运算的算法库之一.Tensorflow由Google开发,是GitHub ...

  2. curl 和 tcpdump

    curl: 1.通常使用curl 来监控网址状态, #curl -m 5 -s -o /dev/null -w %{http_code} www.baidu.com #200 -m 设置访问超时时间, ...

  3. pt-query-digest 慢日志监控

    一.安装percona-toolkit,以centos为例 yum -y install https://repo.percona.com/yum/percona-release-latest.noa ...

  4. light oj 1095 - Arrange the Numbers排列组合(错排列)

    1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...

  5. JS用map做接口数据映射

    · 接口做数据映射的好处:前端页面自定义的字段不需要和后端字段相同,后端修改字段前端不受影响. · 从接口得到数据 res: let r = res.map(item => { return { ...

  6. docker容器互联,实现目录、服务共享

    一.需求 docker使服务之间实现容器隔离,比如Javaweb项目前端.后端.数据库.数据库后台,分别把它们部署在不同的容器里面,实现隔离.但服务和服务之间也有互访的需求,这就涉及到容器网络和容器互 ...

  7. C语言switch中case后跟随break语句

    1.case后面的常量表达式实际上只起语句标号作用,而不起条件判断作用,即“只是开始执行处的入口标号”.因此,一旦与switch后面圆括号中表达式的值匹配,就从此标号处开始执行:而且执行完一个case ...

  8. 学Python必背的初级单词,你都背了吗?

    今天给大家分享一些学习Python必须认识的英文单词,同时也是学习编程都必须会的单词,新手赶快学起来!有点长耐心看完. 小编推荐一个学Python的学习裙:九三七六六七 五零九,无论你是大牛还是小白, ...

  9. 11种常用css样式之表格和定位样式学习

    table表格中border-collapse: collapse;/*表格边框是否合并*/border-spacing: 10px;/*表格边框之间的距离*/定位详情可以阅读position属性值4 ...

  10. 使用Gradle推送SpringBoot项目源码到私有仓库

    应用场景: 在SpringCloud微服务项目中,通常会划分成多个业务服务,而这些服务之间一般会使用Feign组件进行相互调用,所以在项目开发中会衍生出一个问题:Feign客户端代码该由服务调用方的开 ...