DFS-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
Input3
1 2 3Output2Input2
1 5Output4Note
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的更多相关文章
- CF1285 --- Dr. Evil Underscores
CF1285 --- Dr. Evil Underscores 题干 Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_ ...
- CF1285D Dr. Evil Underscores
挂个链接 Description: 给你 \(n\) 个数 \(a_1,a_2,--,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小 ...
- C - Dr. Evil Underscores CodeForces - 1285D 二进制
题目大意:n个数,任意整数x对这n个数取异或值,然后使最大值最小. 思路:数据范围最大为pow(2,30);所以考虑二进制的话,最多有30位.对于某一位d,然后考虑数组v中每一个元素的d为是0还是1, ...
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- Codeforces Round #613 (Div. 2) A-E简要题解
contest链接:https://codeforces.com/contest/1285 A. Mezo Playing Zoma 签到 #include<iostream> #incl ...
- 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 ...
- 图--DFS求连通块
The GeoSurvComp geologic survey company is responsible for detecting u ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
随机推荐
- k8s Pipline CI/CD
一.Pipeline介绍 pipeline是一套jenkins官方提供的插件,它可以用来在jenkins中实现和集成连续交付 用户可以利用Pipeline的许多功能: 代码:pipeline在代码中实 ...
- k8s系列---StorageClass
介绍这个概念前,需要提前知道存储卷pv/pvc之类的概念. 之前的文章有关于EFK日志系统的介绍,里面的环境是测试环境,完全按照教程一步步的操作,甚至注释掉了持久化存储,当真正线上部署时,又抓虾,打开 ...
- 1215 - Finding LCM
1215 - Finding LCM LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...
- asp.net mvc项目实战遇见问题及解决方式----ajax请求500错误,请求多表数据
ajax请求出现500错误——但是想实现的功能是,把一个页面分成了两份,点击右边导航栏,利用ajax请求,请求数据,在右边出现相应页面,当时使用的是partialAction然后出现了这个500错误, ...
- js 常用总结
1.截取字符串 var a="/s/d"; console.log(a.substr(0,a.indexOf("/",1))) // 得到/s 2. // ...
- href的几个特殊值
a href ="" :默认打开的还是当前页面,会刷新一下重新打开. a href ="#": 浏览器地址栏网址后面会多显示1个#.不会刷新页面,会回到页面顶部 ...
- 05.JS函数
前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数9.函数——f ...
- App工程结构
在经过千辛万苦各种填坑终于安装好了Android Studio之后,在其自带的模拟器上成功运行了第一个APP(hello world),通过这个APP首先研究了一下APP基本的工程结构,从而使后面的开 ...
- 《自拍教程19》aapt_apk信息查看工具
aapt命令行工具介绍 aapt.exe(Linux/Ubuntu/imac操作系统下是未带后缀的aapt), 是android sdk自带的用于打包apk,解析apk的命令行工具软件. aapt.e ...
- Java对象拷贝备忘
列举 //cglib net.sf.cglib.beans.BeanCopier.create net.sf.cglib.beans.BeanCopier.copy //spring-beans or ...