#include "doll.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> ord(int lg) {
int N = 1 << lg;
vector<int> ans(N), s(N);
int x = 1, t = 0;
while (1) {
if (x >= N) {
ans[x - N] = t++;
if (x == 2 * N - 1) {
break;
}
x = 1;
} else {
if (s[x] == 0) {
s[x] = 1;
x = x * 2;
} else {
s[x] = 0;
x = x * 2 + 1;
}
}
}
return ans;
}
void create_circuit(int M, vector<int> A) {
int N = A.size();
if (N == 16) {
vector<int> C(M + 1, -1), X(15), Y(15);
for (int i = 0; i < 7; i++) {
X[i] = ~(2 * i + 1);
Y[i] = ~(2 * i + 2);
}
C[0] = A[0];
auto a = ord(4);
for (int i = 0; i < 8; i++) {
X[i + 7] = A[a[i * 2] + 1];
Y[i + 7] = i == 7 ? 0 : A[a[i * 2 + 1] + 1];
}
answer(C, X, Y);
return;
}
vector<int> f(M + 1);
for (int i = 0; i < N; i++) {
f[A[i]]++;
}
if (*max_element(f.begin(), f.end()) <= 4) {
vector<int> S1(M + 1), S2(M + 1), S3(M + 1);
int K = 0;
for (int i = 1; i <= M; i++) {
if (f[i] == 2) {
S1[i] = K++;
} else if (f[i] > 2) {
S1[i] = K++;
S2[i] = K++;
S3[i] = K++;
}
}
vector<int> X(K), Y(K), C(M + 1);
for (int i = 1; i <= M; i++) {
if (f[i] > 1) {
C[i] = ~S1[i];
if (f[i] > 2) {
X[S1[i]] = ~S2[i];
Y[S1[i]] = ~S3[i];
if (f[i] == 3) {
Y[S2[i]] = ~S1[i];
}
}
}
}
C[0] = A[0];
vector<int> cnt(M + 1);
for (int i = 0; i < N; i++) {
if (f[A[i]] == 1) {
C[A[i]] = i + 1 < N ? A[i + 1] : 0;
} else if (f[A[i]] == 2) {
if (cnt[A[i]] == 0) {
X[S1[A[i]]] = A[i + 1];
} else {
Y[S1[A[i]]] = i + 1 < N ? A[i + 1] : 0;
}
} else if (f[A[i]] == 3) {
if (cnt[A[i]] == 0) {
X[S2[A[i]]] = A[i + 1];
} else if (cnt[A[i]] == 1) {
X[S3[A[i]]] = A[i + 1];
} else {
Y[S3[A[i]]] = i + 1 < N ? A[i + 1] : 0;
}
} else if (f[A[i]] == 4) {
if (cnt[A[i]] == 0) {
X[S2[A[i]]] = A[i + 1];
} else if (cnt[A[i]] == 1) {
X[S3[A[i]]] = A[i + 1];
} else if (cnt[A[i]] == 2) {
Y[S2[A[i]]] = A[i + 1];
} else {
Y[S3[A[i]]] = i + 1 < N ? A[i + 1] : 0;
}
}
cnt[A[i]]++;
}
answer(C, X, Y);
return;
}
if (M == 1) {
int lg = __lg(N - 1) + 1;
int K = 1 << lg;
vector<int> C{1, -1}, X(K - 1, -1), Y(K - 1, -1);
for (int i = 0; i < (K / 2) - 1; i++) {
X[i] = ~(2 * i + 1);
Y[i] = ~(2 * i + 2);
}
auto a = ord(lg);
for (int i = 0; i < K / 2; i++) {
if (a[i * 2] < N - 1) {
X[i + K / 2 - 1] = 1;
}
if (a[i * 2 + 1] < N - 1) {
Y[i + K / 2 - 1] = 1;
}
}
Y[K - 2] = 0;
answer(C, X, Y);
return;
}
vector<vector<int>> pos(M + 1);
for (int i = 0; i < N; i++) {
pos[A[i]].push_back(i);
}
vector<int> C(M + 1), X, Y;
C[0] = A[0];
for (int i = 1; i <= M; i++) {
if (!f[i]) {
continue;
}
if (f[i] == 1) {
C[i] = pos[i][0] + 1 < N ? A[pos[i][0] + 1] : 0;
continue;
}
int T = X.size();
int lg = __lg(f[i] - 1) + 1;
int K = 1 << lg;
auto a = ord(lg);
X.resize(T + K - 1, ~T);
Y.resize(T + K - 1, ~T);
C[i] = ~T;
for (int j = 0; j < K / 2; j++) {
if (a[j * 2] < f[i]) {
X[T + j + K / 2 - 1] = pos[i][a[j * 2]] + 1 < N ? A[pos[i][a[j * 2]] + 1] : 0;
}
if (a[j * 2 + 1] < f[i]) {
Y[T + j + K / 2 - 1] = pos[i][a[j * 2 + 1]] + 1 < N ? A[pos[i][a[j * 2 + 1]] + 1] : 0;
}
}
}
answer(C, X, Y);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
14 ms |
4308 KB |
Output is correct |
3 |
Correct |
12 ms |
3284 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
8 ms |
1364 KB |
Output is correct |
6 |
Correct |
18 ms |
4764 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
14 ms |
4308 KB |
Output is correct |
3 |
Correct |
12 ms |
3284 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
8 ms |
1364 KB |
Output is correct |
6 |
Correct |
18 ms |
4764 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
33 ms |
5588 KB |
Output is correct |
9 |
Correct |
29 ms |
6132 KB |
Output is correct |
10 |
Correct |
42 ms |
8092 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
14 ms |
4308 KB |
Output is correct |
3 |
Correct |
12 ms |
3284 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
8 ms |
1364 KB |
Output is correct |
6 |
Correct |
18 ms |
4764 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
33 ms |
5588 KB |
Output is correct |
9 |
Correct |
29 ms |
6132 KB |
Output is correct |
10 |
Correct |
42 ms |
8092 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
53 ms |
9372 KB |
Output is correct |
15 |
Correct |
30 ms |
5320 KB |
Output is correct |
16 |
Correct |
45 ms |
7604 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
49 ms |
8512 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
300 KB |
Output is partially correct |
2 |
Correct |
38 ms |
5628 KB |
Output is correct |
3 |
Partially correct |
67 ms |
9232 KB |
Output is partially correct |
4 |
Partially correct |
74 ms |
9796 KB |
Output is partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
300 KB |
Output is partially correct |
2 |
Correct |
38 ms |
5628 KB |
Output is correct |
3 |
Partially correct |
67 ms |
9232 KB |
Output is partially correct |
4 |
Partially correct |
74 ms |
9796 KB |
Output is partially correct |
5 |
Incorrect |
79 ms |
13076 KB |
over 20000000 inversions |
6 |
Halted |
0 ms |
0 KB |
- |