题目链接
题目思路:
01字典树,当前二进制位置都是1的话,就选择1,都是0就选择0,两种都有就取1<<k+最小值。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
const int maxn=1e5+11;
const double eps=0.00000001;
int n,k=1;
ll a[maxn],ti[30*maxn][3];
void insert(ll x){
int p=0;
for(int i=30;i>=0;--i){
int s=x>>i&1;
if(!ti[p][s]) ti[p][s]=k++;
p=ti[p][s];
}
}
ll solve(int p,int k){
if(k<0) return 0;
if(ti[p][1]==0) return solve(ti[p][0],k-1);
if(ti[p][0]==0) return solve(ti[p][1],k-1);
return (1<<k)+min(solve(ti[p][0],k-1),solve(ti[p][1],k-1));
}
int main(){
cin>>n;
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
insert(a[i]);
}
cout<<solve(0,30)<<endl;
}