# |
Author |
Problem |
Lang |
Verdict |
Time |
Memory |
Sent |
Judged |
|
143108028 |
Practice:
VVD4 |
1626D
- 12
|
C++17 (GCC 7-32)
|
Wrong answer on test 3
|
171 ms
|
4 KB
|
2022-01-17 13:43:44 |
2022-01-17 13:43:44 |
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
// int dp[1000001];
int fun(int a){
for(int i=30;i>=0;i--){
if(a&(1<<i)){
if(2*a==(1<<(i+1)))return 0;
return (1<<(i+1))-a;
}
}
return 1;
}
void solve(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
sort(arr,arr+n);
int ans=INT_MAX;
for(int i=n;i>=0;i--){
int c=(n-i);
if(c)
c=n-(int)(lower_bound(arr,arr+n,arr[i])-arr);
for(int j=0;j<30;j++){
int a=(1<<j);
if(a>(n-c))continue;
a=upper_bound(arr,arr+i,arr[a-1])-arr;
int b=(n-c-a);
ans=min(ans,fun(a)+fun(b)+fun(c));
// cout<<a<<" "<<b<<" "<<c<<" "<<fun(a)+fun(b)+fun(c)<<endl;
}
}
cout<<ans<<endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
// t=1;
while(t--)solve();
}
Click to see test details