QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236496 | #2789. Sorting | Camillus | 20 | 0ms | 4000kb | C++20 | 2.9kb | 2023-11-04 00:26:25 | 2023-11-04 00:26:25 |
Judging History
answer
#include "bits/stdc++.h"
#include "sorting.h"
using namespace std;
mt19937 rnd(228);
bool is_sorted(const vector<int> &s) {
int n = (int)s.size();
for (int i = 0; i + 1 < n; i++) {
if (s[i + 1] < s[i]) {
return false;
}
}
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int n = N;
vector<int> p(S, S + N);
vector<int> d(S, S + N);
if (is_sorted(p)) {
return 0;
}
swap(p[X[0]], p[Y[0]]);
vector<int> num(n, -1);
vector<set<int>> cycles;
for (int u = 0; u < n; u++) {
d[p[u]] = u;
if (num[u] != -1) {
continue;
}
cycles.emplace_back();
int x = (int)cycles.size() - 1;
auto &cycle = cycles.back();
int v = u;
while (num[v] == -1) {
num[v] = x;
cycle.insert(v);
v = p[v];
}
}
set<int> not_alone;
for (int u = 0; u < n; u++) {
if (p[u] != u) {
not_alone.insert(u);
}
}
auto do_swap = [&](int u, int v) -> void {
assert(num[u] == num[v]);
not_alone.erase(u);
not_alone.erase(v);
if (p[u] == v || rnd() % 2) {
swap(u, v);
}
swap(d[p[u]], d[p[v]]);
swap(p[u], p[v]);
cycles.emplace_back();
int x = (int)cycles.size() - 1;
auto &old_cycle = cycles[num[u]];
auto &new_cycle = cycles.back();
while (num[u] != x) {
new_cycle.insert(u);
old_cycle.erase(u);
num[u] = x;
u = p[u];
}
if (p[u] != u) {
not_alone.insert(u);
}
if (p[v] != v) {
not_alone.insert(v);
}
};
for (int i = 0; i < M; i++) {
P[i] = Q[i] = 0;
}
for (int i = 1; i < M; i++) {
if (not_alone.empty()) {
return i;
}
if (not_alone.size() == 2) {
int u = *not_alone.begin();
P[i - 1] = u;
Q[i - 1] = p[u];
return i;
}
if (X[i] == Y[i]) {
int u = *not_alone.begin();
P[i - 1] = u;
Q[i - 1] = p[u];
do_swap(u, p[u]);
} else if (num[X[i]] == num[Y[i]]) {
if (not_alone.size() == 2) {
return i + 1;
}
for (int u : not_alone) {
if (u != p[X[i]] && u != p[Y[i]]) {
P[i - 1] = u;
Q[i - 1] = p[u];
do_swap(u, p[u]);
break;
}
}
do_swap(X[i], Y[i]);
} else {
int x = X[i];
int y = Y[i];
if (p[x] == x && p[y] == y) {
int u = *not_alone.begin();
P[i - 1] = u;
Q[i - 1] = p[u];
do_swap(u, p[u]);
cycles[num[x]].insert(y);
cycles[num[y]].erase(y);
num[y] = num[x];
swap(d[p[x]], d[p[y]]);
swap(p[x], p[y]);
not_alone.insert(x);
not_alone.insert(y);
} else {
if (d[y] != y) {
swap(x, y);
}
P[i - 1] = x;
Q[i - 1] = p[x];
do_swap(x, d[x]);
cycles[num[x]].insert(y);
cycles[num[y]].erase(y);
num[y] = num[x];
swap(d[p[x]], d[p[y]]);
swap(p[x], p[y]);
not_alone.insert(x);
not_alone.insert(y);
}
}
}
return M;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 8
Accepted
Test #1:
score: 8
Accepted
time: 0ms
memory: 3800kb
input:
1 0 1 0 0
output:
0
result:
ok correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
2 0 1 4 0 0 0 0 0 0 0 0
output:
0
result:
ok correct
Test #3:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
2 1 0 4 0 0 0 0 0 0 0 0
output:
1 0 1
result:
ok correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3796kb
input:
3 1 0 2 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
1 0 1
result:
ok correct
Test #5:
score: 0
Accepted
time: 0ms
memory: 3708kb
input:
4 3 2 0 1 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
3 0 3 0 1 0 2
result:
ok correct
Test #6:
score: 0
Accepted
time: 0ms
memory: 3708kb
input:
5 1 4 2 3 0 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
2 0 1 0 4
result:
ok correct
Test #7:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
5 4 2 1 0 3 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
3 0 4 0 3 1 2
result:
ok correct
Subtask #2:
score: 12
Accepted
Test #8:
score: 12
Accepted
time: 0ms
memory: 4000kb
input:
1 0 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
0
result:
ok correct
Test #9:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
2 0 1 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
output:
0
result:
ok correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
98 82 70 31 12 27 51 84 90 66 8 49 52 74 91 46 7 96 67 63 85 34 50 87 83 58 78 26 39 77 48 2 55 94 25 61 56 53 13 32 86 72 20 37 73 9 93 65 28 18 11 71 59 88 35 76 40 24 36 33 3 17 29 38 5 21 15 79 30 62 92 45 80 64 95 43 75 97 23 16 57 22 60 41 69 0 42 14 10 47 68 19 4 1 6 44 81 54 89 2940 0 0 0 0 ...
output:
93 0 82 0 41 0 20 0 34 0 61 0 29 0 48 0 18 0 63 0 5 0 51 0 59 0 3 0 12 0 74 0 43 0 73 0 95 0 81 0 60 0 17 0 67 0 30 0 2 0 31 0 55 0 40 0 72 0 64 0 21 0 50 0 71 0 80 0 22 0 87 0 10 0 49 0 11 0 52 0 88 0 47 0 28 0 77 0 23 0 83 0 69 0 92 0 1 0 70 0 45 0 93 0 6 0 84 4 27 4 39 4 86 4 14 4 46 4 65 4 15 4 ...
result:
ok correct
Test #11:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
99 8 14 3 0 98 74 18 10 50 93 49 23 80 2 54 79 90 31 66 38 81 36 42 83 39 7 9 71 59 62 67 4 68 5 70 72 34 95 25 27 45 61 97 48 29 15 46 12 87 22 26 63 77 84 11 85 30 24 40 78 41 1 96 69 94 19 6 65 33 13 52 75 88 82 86 20 92 43 89 47 28 73 35 32 53 60 21 16 37 58 44 76 57 56 17 55 91 51 64 2970 0 0 0...
output:
92 0 8 0 50 0 26 0 9 0 93 0 56 0 30 0 67 0 65 0 19 0 38 0 25 0 7 0 10 0 49 0 22 0 42 0 97 0 51 0 63 0 69 0 13 0 2 0 3 1 14 1 54 1 11 1 23 1 83 1 32 1 68 1 33 1 5 1 74 1 86 1 21 1 36 1 34 1 70 1 52 1 77 1 43 1 48 1 87 1 16 1 90 1 44 1 29 1 62 1 96 1 91 1 76 1 92 1 57 1 24 1 39 1 27 1 71 1 75 1 20 1 8...
result:
ok correct
Test #12:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
96 12 91 75 59 44 30 52 63 41 13 93 79 82 60 85 78 50 53 57 8 80 67 34 24 87 17 47 33 83 21 81 40 89 49 32 70 26 9 43 7 11 37 94 36 23 28 90 42 35 25 62 56 20 84 55 45 46 22 77 51 0 69 1 31 54 2 3 58 88 76 5 6 65 92 39 66 15 95 10 27 14 86 72 4 68 18 38 19 64 73 71 74 48 29 61 16 2880 0 0 0 0 0 0 0 ...
output:
95 0 12 0 82 0 72 0 65 0 2 0 75 0 66 0 3 0 59 0 51 0 56 0 46 0 90 0 71 0 6 0 52 0 20 0 80 0 14 0 85 0 18 0 57 0 22 0 34 0 32 0 89 0 73 0 92 0 48 0 35 0 70 0 5 0 30 0 81 0 86 0 38 0 43 0 36 0 26 0 47 0 42 0 94 0 61 0 69 0 76 0 15 0 78 0 10 0 93 0 29 0 21 0 67 0 58 0 77 0 95 0 16 0 50 0 62 0 1 0 91 0 ...
result:
ok correct
Subtask #3:
score: 0
Runtime Error
Test #13:
score: 16
Accepted
time: 0ms
memory: 3996kb
input:
2 0 1 60 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
output:
0
result:
ok correct
Test #14:
score: -16
Runtime Error
input:
5 0 4 1 3 2 150 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 ...
output:
Unauthorized output
result:
Subtask #4:
score: 0
Skipped
Dependency #2:
100%
Accepted
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #28:
score: 0
Runtime Error
input:
1800 530 1775 466 953 230 1179 944 752 990 1316 275 1029 158 152 1673 1706 172 115 599 1661 131 699 1112 454 551 1059 291 495 28 67 773 480 839 462 1210 757 879 285 439 3 1429 820 26 1023 942 199 356 625 1705 1421 144 1529 716 7 1485 1027 1599 696 517 1353 456 1389 273 1363 1414 1177 718 41 777 1621...
output:
Unauthorized output
result:
Subtask #6:
score: 0
Skipped
Dependency #5:
0%