#include "sorting.h"
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int findSwapPairs(int N, int SS[], int M, int X[], int Y[], int P[], int Q[]) {
int A[N], pos[N], sop[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;
stack<int>s;
for (int j = 0; j < N; ++j)
pos[A[j]] = j, sop[S[j]] = j, s.push(j);
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(pos[A[X[i]]], pos[A[Y[i]]]);
swap(sop[S[X[i]]], sop[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;
}
while (s.size() && sop[s.top()] == pos[s.top()])
s.pop();
if (s.size()) {
Q[i] = sop[s.top()];
P[i] = pos[s.top()];
s.pop();
}
if (S[Q[i]] != Q[i])
bad--;
if (S[P[i]] != P[i])
bad--;
swap(S[Q[i]], S[P[i]]);
swap(sop[S[Q[i]]], sop[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:12:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int m = tl + tr >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 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 |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 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 |
304 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 |
296 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
692 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
3 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
3 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
310 ms |
17116 KB |
Output is correct |
16 |
Correct |
381 ms |
25156 KB |
Output is correct |
17 |
Correct |
488 ms |
26620 KB |
Output is correct |
18 |
Correct |
91 ms |
21384 KB |
Output is correct |
19 |
Correct |
290 ms |
26208 KB |
Output is correct |
20 |
Correct |
336 ms |
26764 KB |
Output is correct |
21 |
Correct |
335 ms |
26860 KB |
Output is correct |
22 |
Correct |
296 ms |
21300 KB |
Output is correct |
23 |
Correct |
364 ms |
27696 KB |
Output is correct |
24 |
Correct |
511 ms |
27176 KB |
Output is correct |
25 |
Correct |
522 ms |
26820 KB |
Output is correct |
26 |
Correct |
323 ms |
26968 KB |
Output is correct |
27 |
Correct |
380 ms |
26056 KB |
Output is correct |
28 |
Correct |
482 ms |
27108 KB |
Output is correct |
29 |
Correct |
425 ms |
26728 KB |
Output is correct |
30 |
Correct |
232 ms |
25784 KB |
Output is correct |
31 |
Correct |
448 ms |
27300 KB |
Output is correct |
32 |
Correct |
345 ms |
26308 KB |
Output is correct |
33 |
Correct |
447 ms |
27132 KB |
Output is correct |
34 |
Correct |
389 ms |
27056 KB |
Output is correct |
35 |
Correct |
346 ms |
25680 KB |
Output is correct |
36 |
Correct |
99 ms |
22932 KB |
Output is correct |
37 |
Correct |
475 ms |
28004 KB |
Output is correct |
38 |
Correct |
443 ms |
26960 KB |
Output is correct |
39 |
Correct |
450 ms |
27152 KB |
Output is correct |