D. Dr. Evil Underscores
D. Dr. Evil Underscores
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Today, as a friendship gift, Bakry gave Badawy n integers a1,a2,…,an and challenged him to choose an integer X such that the value 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).
Input
The first line contains integer n (1≤n≤105).
The second line contains n integers a1,a2,…,an (0≤ai≤230−1).
Output
Print one integer — the minimum possible value of max1≤i≤n(ai⊕X).
Examples
inputCopy
3
1 2 3
outputCopy
2
inputCopy
2
1 5
outputCopy
4
Note
In the first sample, we can choose X=3.
In the second sample, we can choose X=5.
思路:在每一个数都为1或0时可以不用管这一位,如果都有则选择选0/1两者之间最小的就行了:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int n;
vector<int>q;
int dfs(int n,vector<int> &q){
vector<int>v1,v2;
if(n<0) return 0;
for(int i=0;i<q.size();i++){
if((q[i]>>n)&1) v2.push_back(q[i]);
else v1.push_back(q[i]);
}
if(v1.size()==0) return dfs(n-1,v2);
else if(v2.size()==0) return dfs(n-1,v1);
else return min(dfs(n-1,v1),dfs(n-1,v2))+(1<<n);
}
int main()
{
cout<<(1^2^3^3)<<endl;
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
int a;scanf("%d",&a);
q.push_back(a);
}
printf("%d\n",dfs(30,q));
}
return 0;
}