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. Starting MySQL... ERROR! The server quit without updating PID file (/home/mysql-5.6.43/data/localhost.localdomain.pid).

    启动MySQL出现如下错误 May :: localhost mysqld: Starting MySQL... ERROR! The server quit without updating PID ...

  2. 【GET TIPS】Chrome所见即所得的截图技巧

    精简的前言: 对,我就是想说下事情的来龙去脉.您要不想听,就把耳朵捂起来23333. 想截个新冠肺炎病毒,全国确诊人数今日增长的图,以确定非湖北地区不再明显增长. 但由于网页需要的内容分布在两页,需要 ...

  3. PAT-1005 Spell It Right 解答(C/C++/Java/python)

    1.Description: Given a non-negative integer N, your task is to compute the sum of all the digits of  ...

  4. 安全扫描工具Acunetix即AWVS_13.x系列破解版Linux & Windows

    本站所提供工具仅供技术学习交流.请勿用于非法行为.否则后果自负. Acunetix,自动化网络应用安全软件的先驱,已经宣布发布Acunetix第13版.新版本提供了一个改进的用户界面,并引入了创新,如 ...

  5. idea如何做到多模块开发项目 收藏整理

    idea如何做到多模块开发项目 <packaging>pom</packaging>是什么意思? idea 快捷键汇总

  6. Git操作:一次性强制push所有分支

    现在手上有两个分支,master和rotation,想一次性推送所有分支,可以用--all参数来实现: git push --all origin 如果远程仓库有更改,但你需要直接推送,那就可以使用强 ...

  7. 为NuGet配置微软官方中国镜像

    NuGet微软官方中国镜像地址: https://nuget.cdn.azure.cn/v3/index.json 打开Visual Studio => 工具 => NuGet包管理器 = ...

  8. RAID | 故障处理

    RAID | 故障处理 Unconfigured(good), Spun Up 背景:磁盘替换后在导入外部配置时提示失败,磁盘状态如题. MegaCli -pdgetmissing -a0查看miss ...

  9. npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules

    Mac 权限不够  前面加sudo   然后输入密码

  10. 各大原厂看好MRAM发展

    MRAM是一种以电阻为存储方式结合非易失性及随机访问两种特性,可以兼做内存和硬盘的新型存储介质.写入速度可达NAND闪存的数千倍,此外,其制作工艺要求低,良品率高,可以很好的控制成本.在寿命方面,由于 ...