#include "sorting.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
fill(P, P + M, 0);
fill(Q, Q + M, 0);
if(is_sorted(S, S + N)) {
return 0;
}
int pl = 0, pr = M;
while(pr - pl > 1) {
int pm = (pl + pr) >> 1;
vector<int> T(S, S + N);
for(int i = 0; i < pm; ++i) {
swap(T[X[i]], T[Y[i]]);
}
vector<bool> vis(N, false);
int cycles = 0;
for(int i = 0; i < N; ++i) {
if(vis[i]) continue;
int pos = i;
++cycles;
while(!vis[pos]) {
vis[pos] = true;
pos = T[pos];
}
}
int steps = N - cycles;
if(pm >= steps) pr = pm;
else pl = pm;
}
int L = pr;
for(int i = 0; i < L; ++i) {
swap(S[X[i]], S[Y[i]]);
}
vector<bool> vis2(N, false);
int cl = 0;
for(int i = 0; i < N; ++i) {
if(vis2[i]) continue;
vector<int> path;
int pos = i;
while(!vis2[pos]) {
path.push_back(pos);
vis2[pos] = true;
pos = S[pos];
}
for(int j = int(path.size()) - 1; j >= 1; --j) {
P[cl] = path[j - 1];
Q[cl] = path[j];
++cl;
}
}
vector<int> perm(N), iperm(N);
for(int i = 0; i < N; ++i) {
perm[i] = i;
iperm[perm[i]] = i;
}
for(int i = L - 1; i >= 0; --i) {
P[i] = iperm[P[i]];
Q[i] = iperm[Q[i]];
swap(iperm[perm[X[i]]], iperm[perm[Y[i]]]);
swap(perm[X[i]], perm[Y[i]]);
}
return L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
137 ms |
11744 KB |
Output is correct |
16 |
Correct |
148 ms |
14464 KB |
Output is correct |
17 |
Correct |
168 ms |
14016 KB |
Output is correct |
18 |
Correct |
46 ms |
10872 KB |
Output is correct |
19 |
Correct |
144 ms |
12924 KB |
Output is correct |
20 |
Correct |
154 ms |
13296 KB |
Output is correct |
21 |
Correct |
151 ms |
13204 KB |
Output is correct |
22 |
Correct |
136 ms |
13984 KB |
Output is correct |
23 |
Correct |
156 ms |
14188 KB |
Output is correct |
24 |
Correct |
169 ms |
14096 KB |
Output is correct |
25 |
Correct |
163 ms |
13864 KB |
Output is correct |
26 |
Correct |
152 ms |
13288 KB |
Output is correct |
27 |
Correct |
135 ms |
13044 KB |
Output is correct |
28 |
Correct |
168 ms |
15336 KB |
Output is correct |
29 |
Correct |
165 ms |
13752 KB |
Output is correct |
30 |
Correct |
115 ms |
13172 KB |
Output is correct |
31 |
Correct |
166 ms |
13808 KB |
Output is correct |
32 |
Correct |
154 ms |
15232 KB |
Output is correct |
33 |
Correct |
165 ms |
14096 KB |
Output is correct |
34 |
Correct |
166 ms |
13976 KB |
Output is correct |
35 |
Correct |
145 ms |
12848 KB |
Output is correct |
36 |
Correct |
70 ms |
13168 KB |
Output is correct |
37 |
Correct |
171 ms |
15824 KB |
Output is correct |
38 |
Correct |
168 ms |
15256 KB |
Output is correct |
39 |
Correct |
164 ms |
15480 KB |
Output is correct |