#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5+5;
int n, m;
int x[N], y[N], pos[N], a[N], orig[N];
vector<pair<int, int>> v, v1;
bool check(int mid){
v.clear();v1.clear();
for(int i=0;i<mid;i++){
swap(a[x[i]], a[y[i]]);
}for(int i=0;i<n;i++)pos[a[i]] = i;
int ans = 0;
for(int i=0;i<n;i++){
if(pos[i] == i){
continue;
}else {
ans++;
int f = i, s = pos[i];
v1.push_back({a[f], a[s]});
swap(a[f], a[s]);
swap(pos[a[f]], pos[a[s]]);
}
}
if(ans > mid)return 0;
int j=0;
for(int i=0;i<n;i++)a[i] = orig[i], pos[orig[i]] = i;
for(int i=0;i<mid;i++){
if(j >= v1.size())v.push_back({0,0});
else {
swap(a[x[i]], a[y[i]]);
swap(pos[a[x[i]]], pos[a[y[i]]]);
int f = v1[j].first, s = v1[j].second;
v.push_back({pos[f], pos[s]});
swap(a[pos[f]], a[pos[s]]);
swap(pos[a[pos[f]]], pos[a[pos[s]]]);
}j++;
}return 1;
}
int findSwapPairs(int NN, int A[], int MM, int X[], int Y[], int P[], int Q[]) {
n = NN, m = MM;
int l = -1, r = m;
for(int i=0;i<m;i++){
x[i] = X[i];
y[i] = Y[i];
}
while(l+1 < r){
for(int i=0;i<n;i++)a[i] = A[i], orig[i] = A[i];
int mid = (l+r)/2;
if(check(mid)){
r = mid;
}else {
l = mid;
}
}for(int i=0;i<n;i++)a[i] = A[i];
check(r);
for(int i=0;i<r;i++){
if(i < v.size()){
P[i] = v[i].first;
Q[i] = v[i].second;
}else {
P[i] = Q[i] = 0;
}
}
return r;
}
Compilation message
sorting.cpp: In function 'bool check(int)':
sorting.cpp:29:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(j >= v1.size())v.push_back({0,0});
| ~~^~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:58:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(i < v.size()){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 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 |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 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 |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 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 |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
241 ms |
18784 KB |
Output is correct |
16 |
Correct |
292 ms |
26336 KB |
Output is correct |
17 |
Correct |
306 ms |
27104 KB |
Output is correct |
18 |
Correct |
125 ms |
24424 KB |
Output is correct |
19 |
Correct |
218 ms |
25336 KB |
Output is correct |
20 |
Correct |
238 ms |
25952 KB |
Output is correct |
21 |
Correct |
223 ms |
25952 KB |
Output is correct |
22 |
Correct |
251 ms |
23904 KB |
Output is correct |
23 |
Correct |
274 ms |
27876 KB |
Output is correct |
24 |
Correct |
339 ms |
27616 KB |
Output is correct |
25 |
Correct |
340 ms |
27236 KB |
Output is correct |
26 |
Correct |
223 ms |
25304 KB |
Output is correct |
27 |
Correct |
203 ms |
24540 KB |
Output is correct |
28 |
Correct |
295 ms |
27184 KB |
Output is correct |
29 |
Correct |
315 ms |
26720 KB |
Output is correct |
30 |
Correct |
160 ms |
25192 KB |
Output is correct |
31 |
Correct |
323 ms |
27984 KB |
Output is correct |
32 |
Correct |
279 ms |
28128 KB |
Output is correct |
33 |
Correct |
338 ms |
28384 KB |
Output is correct |
34 |
Correct |
294 ms |
28512 KB |
Output is correct |
35 |
Correct |
227 ms |
26208 KB |
Output is correct |
36 |
Correct |
86 ms |
24936 KB |
Output is correct |
37 |
Correct |
333 ms |
29000 KB |
Output is correct |
38 |
Correct |
314 ms |
28128 KB |
Output is correct |
39 |
Correct |
328 ms |
28256 KB |
Output is correct |