https://lydsy.com/JudgeOnline/problem.php?id=5362
分析:
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 300050
typedef long long ll;
int n,a[N],k=30;
ll ans;
void solve(int l,int r,int d) {
if(d==-1||l>=r) return ;
if(l==r-1) {ans+=a[l]^a[r]; return ;}
int mid=l,i,j;
for(;mid<=r&&!((a[mid]>>d)&1);mid++) ;
solve(l,mid-1,d-1);
solve(mid,r,d-1);
int ls=mid-l,rs=r-mid+1;
if(ls>2&&rs>2) return ;
if(ls<1||rs<1) return ;
int mn1=1<<30,mn2=1<<30;
for(i=l;i<mid;i++) {
for(j=mid;j<=r;j++) {
int x=a[i]^a[j];
if(x<mn1) mn2=mn1,mn1=x;
else if(x<mn2) mn2=x;
}
}
if(ls<=2&&rs<=2) ans+=mn1+mn2;
else ans+=mn1;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int i;
for(i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
ans=0;
solve(1,n,k);
printf("%lld\n",ans);
}
}
BZOJ5362: [Lydsy1805月赛]quailty 算法
原文:https://www.cnblogs.com/suika/p/10165821.html