forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1742G.cpp
179 lines (178 loc) · 3.85 KB
/
1742G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define Fori(x) for(int i=0;i<(x);i++)
#define gcd(a,b) __gcd(a,b)
#define pb(x) push_back(x)
#define L length()
#define mkp(x,y) make_pair(x,y)
#define int long long
#define bs binary_search
#define mod 1e9+7
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define allr(v) v.rbegin(),v.rend1()
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define pii pair<int,int>
#define vi vector<int>
#define vs size()
#define all(v) v.begin(),v.end()
#define endl '\n'
#define atoi stoi
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
#define si set <int>
#define vpii vector < pair <int,int> >
#define memf(a) memset(a,false,sizeof(a))
#define memt(a) memset(a,true,sizeof(a))
#define xxx 998244353
#define pi 3.141592653589
#define ninf INT_MIN
#define inf 1e18
#define sz(v) ((int)(v).size())
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define FILL(a,x) memset(a,x,sizeof(a))
int max(int a,int b){if(a>b){return a;}else{return b;}}
int min(int a,int b){if(a<b){return a;}else{return b;}}
int rand_int(int l, int r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<int>(l, r)(gen);
}
int power(int b,int e)
{
if(e==0)
return 1;
if(e%2)
return ((b*power((b)*(b),(e-1)/2)));
else
return power((b)*(b),e/2);
}
int modpower(int b,int e,int q)
{
int MOD=q;
if(e==0)
return 1;
if(e%2)
return ((b%MOD)*modpower((b%MOD)*(b%MOD),(e-1)/2,q))%MOD;
else
return modpower((b%MOD)*(b%MOD),e/2,q)%MOD;
}
void dpv(vi v)
{
for(int i=0;i<v.vs;i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
void dpv(vpii v)
{
for(int i=0;i<v.vs;i++)
{
cout<<v[i].F<<" "<<v[i].S<<endl;
}
}
void dpv(set <int> v)
{
for(auto i:v)
{
cout<<i<<" ";
}
cout<<endl;
}
int ceil1(int x,int y)
{
int r=x/y;
if(x%y==0)
{
return r;
}
else
{
return r+1;
}
}
////*SOLUTION TO THE QUESTION STARTS HERE*//////
//const int N=2e5+5;
void oblivious()
{
int n;
cin>>n;
set<int> st;
for(int i=0;i<=31;i++){
st.insert(i);
}
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
vector<int> ans;
vector<int> used(n,0);
for(int i=0;i<31;i++){
if(st.empty()){
break;
}
int val=0;
for(auto j:st){
val+=(1ll<<j);
}
if(val==0){
break;
}
int mx=-1;
int ix=-1;
for(int j=0;j<n;j++){
if(used[j]){
continue;
}
if((val&a[j]) > mx){
mx=val&a[j];
ix=j;
}
}
if(mx==-1){
break;
}
used[ix]=1;
ans.push_back(a[ix]);
for(int j=0;j<31;j++){
if((a[ix]>>j)&1){
st.erase(j);
}
}
}
for(int i=0;i<n;i++){
if(used[i]){
continue;
}
ans.push_back(a[i]);
}
dpv(ans);
}
signed main()
{
IOS;
//FILL(dp,0);
//Try this case
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
int t=1;
cin>>t;
//int z=t;
while(t--)
{
//cout<<"Case #"<<(z-t)<<": ";
oblivious();
}
return 0;
}
//Editorial
/*
//*/