QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236453 | #2789. Sorting | Camillus | 0 | 1ms | 3872kb | C++20 | 2.1kb | 2023-11-03 23:32:45 | 2023-11-03 23:32:45 |
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[0], Q[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(p[u], p[v]);
swap(d[p[u]], d[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(x);
old_cycle.erase(x);
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 (cycles.size() == n) {
return i;
}
if (not_alone.size() == 1) {
int u = *not_alone.begin();
P[i] = Q[i] = u;
return i;
}
if (X[i] == Y[i]) {
int u = *not_alone.begin();
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]]) {
do_swap(u, p[u]);
break;
}
}
do_swap(X[i], Y[i]);
} else {
}
}
return M;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 8
Accepted
time: 0ms
memory: 3576kb
input:
1 0 1 0 0
output:
0
result:
ok correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
2 0 1 4 0 0 0 0 0 0 0 0
output:
0
result:
ok correct
Test #3:
score: -8
Wrong Answer
time: 0ms
memory: 3592kb
input:
2 1 0 4 0 0 0 0 0 0 0 0
output:
2 0 0 0 0
result:
wrong answer the final array is not sorted
Subtask #2:
score: 0
Wrong Answer
Test #8:
score: 12
Accepted
time: 0ms
memory: 3596kb
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: 3584kb
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: -12
Wrong Answer
time: 1ms
memory: 3636kb
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:
94 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 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...
result:
wrong answer the final array is not sorted
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 16
Accepted
time: 0ms
memory: 3584kb
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
Wrong Answer
time: 1ms
memory: 3696kb
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:
150 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 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 ...
result:
wrong answer the final array is not sorted
Subtask #4:
score: 0
Skipped
Dependency #2:
0%
Subtask #5:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 0ms
memory: 3872kb
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:
5400 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 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...
result:
wrong answer the final array is not sorted
Subtask #6:
score: 0
Skipped
Dependency #5:
0%