#include "sorting.h"
#include <iostream>
#include <algorithm>
using namespace std;
int findSwapPairs(int N, int SS[], int M, int X[], int Y[], int P[], int Q[]) {
int A[N], pos[N], L[M], R[M], ret = (1<<30), S[N];
int tl = 0, tr = M - 1;
while (tl <= tr) {
int m = tl + tr >> 1;
for (int i = 0; i < N; ++i)
A[i] = i, S[i] = SS[i];
for (int i = m; i >= 0; --i)
swap(A[X[i]], A[Y[i]]);
int bad = 0;
for (int i = 0; i < N; ++i)
if (S[i] != i)
bad++;
int r = -1;
for (int i = 0; i <= m; ++i) {
P[i] = Q[i] = 0;
if (bad == 0) {
r = i;
break;
}
if (S[X[i]] != X[i])
bad--;
if (S[Y[i]] != Y[i])
bad--;
swap(S[X[i]], S[Y[i]]);
swap(A[X[i]], A[Y[i]]);
if (S[X[i]] != X[i])
bad++;
if (S[Y[i]] != Y[i])
bad++;
if (bad == 0) {
r = i + 1;
break;
}
for (int j = 0; j < N; ++j)
pos[A[j]] = j;
for (int j = 0; j < N; ++j) {
if (pos[S[j]] != j) {
Q[i] = j, P[i] = pos[S[j]];
break;
}
}
if (S[Q[i]] != Q[i])
bad--;
if (S[P[i]] != P[i])
bad--;
swap(S[Q[i]], S[P[i]]);
if (S[Q[i]] != Q[i])
bad++;
if (S[P[i]] != P[i])
bad++;
}
if (bad) {
tl = m + 1;
continue;
}
tr = m - 1;
if (r == -1) r = m + 1;
if (ret >= r) {
ret = r;
for (int i = 0; i < r; ++i)
L[i] = Q[i], R[i] = P[i];
}
}
for (int i = 0; i < ret; ++i)
Q[i] = L[i], P[i]= R[i];
return ret;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:11:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
11 | int m = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
31 ms |
532 KB |
Output is correct |
22 |
Correct |
36 ms |
564 KB |
Output is correct |
23 |
Correct |
35 ms |
588 KB |
Output is correct |
24 |
Correct |
32 ms |
572 KB |
Output is correct |
25 |
Correct |
32 ms |
576 KB |
Output is correct |
26 |
Correct |
29 ms |
544 KB |
Output is correct |
27 |
Correct |
33 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
400 KB |
Output is correct |
2 |
Correct |
134 ms |
412 KB |
Output is correct |
3 |
Correct |
142 ms |
568 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
44 ms |
332 KB |
Output is correct |
6 |
Correct |
58 ms |
428 KB |
Output is correct |
7 |
Correct |
95 ms |
428 KB |
Output is correct |
8 |
Correct |
151 ms |
420 KB |
Output is correct |
9 |
Correct |
142 ms |
420 KB |
Output is correct |
10 |
Correct |
147 ms |
416 KB |
Output is correct |
11 |
Correct |
156 ms |
428 KB |
Output is correct |
12 |
Correct |
103 ms |
424 KB |
Output is correct |
13 |
Correct |
146 ms |
420 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
400 KB |
Output is correct |
2 |
Correct |
134 ms |
412 KB |
Output is correct |
3 |
Correct |
142 ms |
568 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
44 ms |
332 KB |
Output is correct |
6 |
Correct |
58 ms |
428 KB |
Output is correct |
7 |
Correct |
95 ms |
428 KB |
Output is correct |
8 |
Correct |
151 ms |
420 KB |
Output is correct |
9 |
Correct |
142 ms |
420 KB |
Output is correct |
10 |
Correct |
147 ms |
416 KB |
Output is correct |
11 |
Correct |
156 ms |
428 KB |
Output is correct |
12 |
Correct |
103 ms |
424 KB |
Output is correct |
13 |
Correct |
146 ms |
420 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Execution timed out |
1089 ms |
11376 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |