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.
题意:找出一个数X使得最小,并求出这个值。
思路:异或值最大,首先想到的是字典树,但是根据这个思路,如果说这一位异或之后的数字相同,那么这一位就不产生贡献。如果说这一位异或之后的数字不相同,那么就产生贡献(1<<k)。但是我们要求最大值最小,我们只需要看后面的数产生的贡献哪个比较小就可以了。这样我们把这一位是1的,和这一位是0的分成两个集合,分别递归下去,取返回值较小的,再加上(1<<k)就可以了。
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxx=1e5+100;
int n;
inline int dfs(int k,vector<int> &p)
{
if(k<0) return 0;
if(p.size()==0) return 0;
vector<int> p1,p0;
for(int i=0;i<p.size();i++)
{
if((p[i]>>k)&1) p1.push_back(p[i]);
else p0.push_back(p[i]);
}
if(p0.size()==0) return dfs(k-1,p1);
if(p1.size()==0) return dfs(k-1,p0);
return min(dfs(k-1,p1),dfs(k-1,p0))+(1<<k);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
vector<int> p;
int x;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
p.push_back(x);
}
cout<<dfs(30,p)<<endl;
}
return 0;
}
努力加油a啊,(o)/~