温习一下多校的题目
这题主要抓住一点,亦或值的贡献是固定的
所以按位搜索即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+5;
int n;
int A[MAXN];
ll cnt[20][20];
int d[20];
int X;
int Ed;
ll ans;
void dfs(int pos,int x,ll num){
if(num > ans) return;
if(pos == Ed){
if(num < ans) { ans = num; X = x; }
else if(num == ans && X > x) X = x;
return;
}
int i = pos;
for(d[i] = 0; d[i] < 2; d[i] ++){
ll tmp = num + cnt[i][i];
for(int j = 0; j < i; ++j)
if(d[i] ^ d[j])
tmp -= cnt[i][j];
else tmp += cnt[i][j];
dfs(pos+1, x|d[i]<<i, tmp);
}
}
int main(){
int T; scanf("%d",&T);
while(T--){
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
for(int i = 1; i <= n; ++i) scanf("%d",&A[i]);
for(int i = 2; i <= n; ++i) {
int t1 = A[i-1]; int t2 = A[i];
int high = 20;
while(high>=0 && ~(t1^t2)>>high&1 ) high--;
// printf("%d
",high);
int mx = max(t1, t2);
int mi = min(t1, t2);
for(int j = high; j >= 0; --j){
cnt[high][j] += (mx&1<<j) - (mi&1<<j);
}
}
Ed = 20;
for(; Ed >0; Ed--)
if(cnt[Ed-1][Ed-1]) break;
X=0; ans=1e18;
dfs(0,0,0);
printf("%d %lld
",X,ans);
}
return 0;
}