-
Notifications
You must be signed in to change notification settings - Fork 0
/
1984G.cpp
253 lines (214 loc) · 4.78 KB
/
1984G.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#ifdef DBG
#define dbg(...) fprintf(stdout, __VA_ARGS__)
#else
#define dbg(...) \
do { \
} while (0)
#endif
const int MAXN = 1005;
const int MAXM = 5 * MAXN * MAXN;
const int MOD = 1e9 + 7;
int n;
vector<int> p;
vector<pair<int, int>> operations;
bool is_sorted() {
for (int i = 1; i < n; ++i) {
if (p[i] < p[i - 1]) {
return false;
}
}
return true;
}
bool is_cyclic() {
for (int i = 0; i < n; ++i) {
int x = p[i];
int y = p[(i + 1) % n];
if (x + 1 != y && x != n) {
return false;
}
}
return true;
}
struct Arr {
public:
vector<int> arr;
int split;
int &get(int i) { return arr[(i + split) % n]; }
int &first() { return get(0); }
int &last() { return get(n - 1); }
int &last2() { return get(n - 2); }
void output() {
dbg("arr: ");
for (int i = 0; i < n; ++i) {
dbg("%d ", get(i));
}
dbg("\n");
}
// .......xy
// x.......y
void doA() {
operations.push_back({1, 2});
swap(get(n - 1), get(n - 2));
split = (split + n - 1) % n;
dbg("doA: ");
output();
}
// x......y
// ......xy
void doB() {
operations.push_back({2, 1});
swap(get(0), get(n - 1));
split = (split + 1) % n;
dbg("doB: ");
output();
}
// .....xy
// xy.....
void doE() {
operations.push_back({1, 3});
split = (split + n - 2) % n;
dbg("doE: ");
output();
}
int index(int num) {
for (int i = 0; i < n; ++i) {
if (get(i) == num) {
return i;
}
}
assert(false);
}
};
bool is_inv(int x, int y) {
if (x == 1 || y == 1) return false;
return x > y;
}
// solve with k = n - 2
bool solve2(Arr arr) {
dbg("try to solve: ");
arr.output();
int done = 0;
while (done < n + 5) {
if (is_inv(arr.last2(), arr.last())) {
arr.doA();
done = 0;
}
if (is_inv(arr.last(), arr.first())) {
arr.doB();
done = 0;
}
if (done > 0) {
arr.doE();
}
done += 1;
}
int index1 = arr.index(1);
if (n % 2 == 0 && index1 % 2 == 1)
return false;
while (arr.first() != 1) {
arr.doE();
}
return true;
}
void prepare() {
while (p[n - 1] != n && p[n - 2] != n && p[n - 3] != n) {
int a = p[n - 3], b = p[n - 2], c = p[n - 1];
for (int i = n - 1; i >= 3; i--) {
p[i] = p[i - 3];
}
p[0] = a;
p[1] = b;
p[2] = c;
operations.push_back({1, 4});
}
dbg("after prepare\n");
for (int i = 0; i < n; ++i) {
dbg("%d ", p[i]);
}
dbg("\n");
if (p[n - 1] == n) {
dbg("prepare: 1\n");
return;
}
// xxxxxxnx
// -----
if (p[n - 2] == n) {
dbg("prepare: 2\n");
int a = p[n - 1];
for (int i = n - 1; i >= 3; i--) {
p[i] = p[i - 1];
}
p[2] = a;
operations.push_back({3, 4});
return;
}
// xxxxxnxx
// -----
if (p[n - 3] == n) {
dbg("prepare: 3\n");
int a = p[n - 2], b = p[n - 1];
for (int i = n - 1; i >= 3; i--) {
p[i] = p[i - 2];
}
p[1] = a;
p[2] = b;
operations.push_back({2, 4});
return;
}
}
int solve() {
// Implement the solution here, using global variables p and operations
if (is_sorted()) {
return n;
}
if (is_cyclic()) {
while (!is_sorted()) {
operations.push_back({1, 2});
int last = p[n - 1];
for (int i = n - 1; i > 0; i--) {
p[i] = p[i - 1];
}
p[0] = last;
}
return n - 1;
}
Arr arr{p, 0};
if (solve2(arr)) {
return n - 2;
}
operations.clear();
prepare();
n -= 1;
Arr arr2{p, 0};
assert(solve2(arr2));
n += 1;
return n - 3;
}
void reset() {
p.clear();
operations.clear();
// Reset other global variables if needed
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
reset();
scanf("%d", &n);
p.resize(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
}
int k = solve();
printf("%d\n", k);
int m = operations.size();
printf("%d\n", m);
for (const auto &op : operations) {
printf("%d %d\n", op.first, op.second);
}
}
return 0;
}