正解:最小生成树
解题报告:
发现$Kruskal$和$Prime$都不太可做,于是考虑$B$算法.
先大概港下$B$算法的流程趴$QwQ$.大概就,每次对每个联通块找到最近的联通块,连边.一直做下去就好.因为每次联通块个数至少会减少二分之一,所以最多做$logn$次.
然后现在来看这题.考虑倒序模拟$B$算法的过程
于是从高位向低位看,发现若在当前位有1也有0,则两个联通块之间必然连且仅连一条边,剩下的一定是两个联通块内分别连.所以就只要找到两个联通块之间的最短路就行,就直接在$trie$树上插入查询下就行$QwQ$
$over$
#include<bits/stdc++.h> using namespace std; #define il inline #define lf double #define gc getchar() #define mp make_pair #define int long long #define P pair<int,int> #define t(i) edge[i].to #define w(i) edge[i].wei #define ri register int #define rc register char #define rb register bool #define lowbit(x) (x&(-x)) #define rp(i,x,y) for(ri i=x;i<=y;++i) #define my(i,x,y) for(ri i=x;i>=y;--i) #define e(i,x) for(ri i=head[x];i;i=edge[i].nxt) #define lbh(x) lower_bound(sth+1,sth+1+h_cnt,x)-sth #define lbl(x) lower_bound(stl+1,stl+1+l_cnt,x)-stl const int N=2e6+10,inf=1e9,M=30; int n,as,nod_cnt; struct node{int to[2];il void clr(){to[0]=to[1]=0;}}nod[N]; il int read() { rc ch=gc;ri x=0;rb y=1; while(ch!='-' && (ch>'9' || ch<'0'))ch=gc; if(ch=='-')ch=gc,y=0; while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc; return y?x:-x; } il int getnode(){++nod_cnt;nod[nod_cnt].clr();return nod_cnt;} il void insert(ri x){ri nw=0;my(i,M,0){ri t=(x>>i)&1;if(!nod[nw].to[t])nod[nw].to[t]=getnode();nw=nod[nw].to[t];}} il int query(ri x) { ri nw=0,ret=0; my(i,M,0){ri t=(x>>i)&1;if(nod[nw].to[t])nw=nod[nw].to[t];else nw=nod[nw].to[!t],ret+=(1<<i);} return ret; } il void solv(vector<int>V,int pos) { if(!(~pos) || V.empty())return; ri sz=V.size();vector<int>v[2]; rp(i,0,sz-1)v[(V[i]>>pos)&1].push_back(V[i]);ri sz0=v[0].size(),sz1=v[1].size(); if(sz0 && sz1) { nod_cnt=0;nod[0].clr();ri ret=inf; rp(i,0,sz0-1)insert(v[0][i]);rp(i,0,sz1-1)ret=min(ret,query(v[1][i]));;as+=ret; } solv(v[0],pos-1);solv(v[1],pos-1); } signed main() { n=read();vector<int>V;rp(i,1,n)V.push_back(read());solv(V,30);printf("%lld ",as); return 0; }