Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created October 13, 2022 17:03
Show Gist options
  • Save qjatn0120/c0140a4b89210a3f9e253a040e784a70 to your computer and use it in GitHub Desktop.
Save qjatn0120/c0140a4b89210a3f9e253a040e784a70 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int t, n, val, a[MX];
bitset <MX> used;
vector <int> ans;
bool cmp(int x, int y){
x ^= (x & val);
y ^= (y & val);
return x < y;
}
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
val = 0;
used.reset();
for(int i = 0; i < min(40, n); i++){
int maxIdx = -1;
for(int j = 0; j < n; j++){
if(used.test(j)) continue;
if(maxIdx == -1) maxIdx = j;
else if(cmp(a[maxIdx], a[j])) maxIdx = j;
}
used.set(maxIdx);
ans.push_back(a[maxIdx]);
val |= a[maxIdx];
}
for(int i = 0; i < n; i++){
if(!used.test(i)) ans.push_back(a[i]);
}
for(int i : ans) cout << i << " ";
cout << "\n";
ans.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment