#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int n, int arr[], int m, int x_moves[], int y_moves[], int x_ans[], int y_ans[]){
vector<int> fin_arr(n), x_todo, y_todo;
auto calc = [&](int mid)->int{
x_todo.clear(); y_todo.clear();
for(int i = 0; i<n; ++i) fin_arr[i] = arr[i];
for(int i = 0; i<mid; ++i)
swap(fin_arr[x_moves[i]], fin_arr[y_moves[i]]);
for(int i = 0; i<n; ++i)
while(fin_arr[i]!=i)
x_todo.push_back(fin_arr[i]), y_todo.push_back(fin_arr[fin_arr[i]]),
swap(fin_arr[i], fin_arr[fin_arr[i]]);
return (int)x_todo.size();
};
int lo = 0, hi = m;
while(lo<hi){
int mid = lo+(hi-lo)/2;
if(calc(mid)<=mid) hi = mid;
else lo = mid+1;
} calc(lo);
while(x_todo.size()<lo)
x_todo.push_back(0), y_todo.push_back(0);
vector<int> pos(n);
for(int i = 0; i<n; ++i)
pos[arr[i]] = i;
for(int i = 0; i<lo; ++i)
swap(pos[arr[x_moves[i]]], pos[arr[y_moves[i]]]), swap(arr[x_moves[i]], arr[y_moves[i]]),
x_ans[i] = pos[x_todo[i]], y_ans[i] = pos[y_todo[i]],
swap(pos[arr[x_ans[i]]], pos[arr[y_ans[i]]]), swap(arr[x_ans[i]], arr[y_ans[i]]);
return lo;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:23:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
23 | while(x_todo.size()<lo)
| ~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
432 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
428 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
428 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
140 ms |
17144 KB |
Output is correct |
16 |
Correct |
138 ms |
17456 KB |
Output is correct |
17 |
Correct |
151 ms |
18448 KB |
Output is correct |
18 |
Correct |
56 ms |
16320 KB |
Output is correct |
19 |
Correct |
110 ms |
18116 KB |
Output is correct |
20 |
Correct |
117 ms |
18608 KB |
Output is correct |
21 |
Correct |
116 ms |
18636 KB |
Output is correct |
22 |
Correct |
123 ms |
13752 KB |
Output is correct |
23 |
Correct |
158 ms |
19120 KB |
Output is correct |
24 |
Correct |
159 ms |
18996 KB |
Output is correct |
25 |
Correct |
152 ms |
18656 KB |
Output is correct |
26 |
Correct |
111 ms |
18128 KB |
Output is correct |
27 |
Correct |
98 ms |
17360 KB |
Output is correct |
28 |
Correct |
153 ms |
18736 KB |
Output is correct |
29 |
Correct |
148 ms |
18296 KB |
Output is correct |
30 |
Correct |
81 ms |
16960 KB |
Output is correct |
31 |
Correct |
155 ms |
18776 KB |
Output is correct |
32 |
Correct |
144 ms |
18432 KB |
Output is correct |
33 |
Correct |
157 ms |
18864 KB |
Output is correct |
34 |
Correct |
163 ms |
18760 KB |
Output is correct |
35 |
Correct |
116 ms |
17808 KB |
Output is correct |
36 |
Correct |
47 ms |
15812 KB |
Output is correct |
37 |
Correct |
164 ms |
19408 KB |
Output is correct |
38 |
Correct |
157 ms |
18608 KB |
Output is correct |
39 |
Correct |
157 ms |
18756 KB |
Output is correct |