CF888G Xor-MST

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#define ll long long
#define maxn 200500
#define node vector<int>
using namespace std;
int n,ch[maxn*20][2],ori[maxn],tot;
void insert(int num) {
int p=30,now=0;
while(p>=0) {
int digit=0;
if((1<<p)&num)digit=1;
if(!ch[now][digit]){ch[now][digit]=++tot;ch[tot][1]=ch[tot][0]=0;}
now=ch[now][digit];p--;
}
return;
}
int query(int num) {
int p=30;int ans=0;int now=0;
while(p>=0) {
int digit=0;
if(num&(1<<p))digit=1;
if(ch[now][digit])now=ch[now][digit];
else{now=ch[now][digit^1];ans|=(1<<p);}
p--;
}
return ans;
}
ll solve(node a,int p) {
if(p<0||!a.size())return 0;
node b1,b0;
for(int i=0;i<a.size();i++) {
if(a[i]&(1<<p))b1.push_back(a[i]);
else b0.push_back(a[i]);
}
int mi=0;
if(b0.size()&&b1.size()){mi=1e9+1000;tot=0;ch[0][1]=ch[0][0]=0;
for(int i=0;i<b0.size();i++)insert(b0[i]);
for(int i=0;i<b1.size();i++)mi=min(mi,query(b1[i]));}
return mi+solve(b0,p-1)+solve(b1,p-1);
}
int main() {
scanf("%d",&n);node v;for(int i=1;i<=n;i++){scanf("%d",&ori[i]);v.push_back(ori[i]);}
printf("%lld",solve(v,30));
}


posted @ 2019-07-30 19:07  GavinZheng  阅读(222)  评论(0编辑  收藏  举报