-
Notifications
You must be signed in to change notification settings - Fork 17
/
Too Much XOR.cpp
187 lines (168 loc) · 4.27 KB
/
Too Much XOR.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
180
181
182
183
184
185
186
187
#include <bits/stdc++.h> // This will work only for g++ compiler.
#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
//short hand for usual tokens
#define pb push_back
#define fi first
#define se second
// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())
// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())
using namespace std;
// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
void insert_move(vector<vector<long long> >&moves, ll start, ll a, ll b, ll n)
{
for(int i=start;i<n;){
vector<long long> temp;
temp.pb(a+1);
temp.pb(b+1);
temp.pb(i+1);
moves.pb(temp);
i++;
i++;
}
}
void abc1(vector<long long> &value)
{
ll n = value.size();
ll k = 1,x=-1;
for(int i=3;i<n;i+=2){
if(value[i]==value[k]){
continue;
}
else{
x=i;
break;
}
}
vector<vector<long long> >moves;
if(x==-1){
cout<<0<<endl;
return;
}
insert_move(moves, 0, k, x,n);
insert_move(moves, 1, 0, 2,n);
cout<<moves.size()<<endl;
for(int i=0; i<moves.size(); i++){
cout<<moves[i][0]<<" "<<moves[i][1]<<" "<<moves[i][2]<<endl;
}
return;
}
void abc(){
ll n;
cin>>n;
vector<long long> value(n);
for(int i=0;i<n;i++)
cin>>value[i];
if(n==1){
cout<<0<<endl;
return;
}
else if(n==2){
if(value[0]==value[1])
cout<<-1<<endl;
else
cout<<0<<endl;
return;
}
else if(n==3){
if((value[0]==value[2]) && (value[0]!=value[1])){
cout<<0<<endl;
return;
}
else if((value[0]==value[2]) && (value[0]==value[1])){
if(value[0]==0){
cout<<-1<<endl;
return;
}
cout<<1<<endl;
cout<<"1 3 2"<<endl;
return;
}
else if(value[1]==0){
if(value[0]!=0){
cout<<1<<endl;
cout<<"1 2 3"<<endl;
return;
}
if(value[2]!=0){
cout<<1<<endl;
cout<<"3 2 1"<<endl;
return;
}
}
cout<<-1<<endl;
return;
}
int count0 = 0;
for(int i=0; i<n; i++){
if(value[i]==0)
count0++;
}
int count1 = 0;
for(int i=0; i<n; i++){
if(value[i]==1)
count1++;
}
if(count0 == n){
cout<<-1<<endl;
return;
}
ll k = 0,x=-1;
for(int i=2;i<n;i+=2){
if(value[i]==value[k]){
continue;
}
else{
x=i;
break;
}
}
vector<vector<long long> >moves;
if(x==-1){
if(value[0]==0){
abc1(value);
return;
}
insert_move(moves, 1, 0, 2,n);
cout<<moves.size()<<endl;
for(int i=0; i<moves.size(); i++){
cout<<moves[i][0]<<" "<<moves[i][1]<<" "<<moves[i][2]<<endl;
}
return;
}
insert_move(moves, 1, k, x,n);
insert_move(moves, 0, 1, 3,n);
cout<<moves.size()<<endl;
for(int i=0; i<moves.size(); i++){
cout<<moves[i][0]<<" "<<moves[i][1]<<" "<<moves[i][2]<<endl;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
int tc = 1;
cin>>tc;
while(tc--){
abc();
}
return 0;
}