#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int MXN = 2e5 + 100;
int n, m, a[MXN], p[MXN], pos[MXN], tmp[MXN], pos_box[MXN];
bool is_sorted(){
bool good = 1;
for (int i = 1; i < n; i ++)
good &= (a[i] >= a[i - 1]);
return good;
}
int findSwapPairs(int N, int s[], int M, int x[], int y[], int rx[], int ry[]){
n = N, m = M;
for (int i = 0; i < n; i ++)
a[i] = s[i];
if (is_sorted())
return 0;
int l = 0;
int r = n;
while (r - l > 1){
int mid = (l + r) / 2;
for (int i = 0; i < n; i ++)
p[i] = tmp[i] = i, a[i] = s[i], pos[a[i]] = i, pos_box[i] = i;
for (int i = 0; i < mid; i ++)
swap(p[x[i]], p[y[i]]);
int steps = 0;
bool move = 0;
for (int i = 0, j = 0; i < n and j < mid;){
if (!move){
swap(pos_box[tmp[x[j]]], pos_box[tmp[y[j]]]);
swap(tmp[x[j]], tmp[y[j]]);
swap(pos[a[x[j]]], pos[a[y[j]]]);
swap(a[x[j]], a[y[j]]);
move = 1;
continue;
}
if (pos[i] == pos_box[p[i]]){
i++;
continue;
}
move = 0;
rx[j] = pos[i], ry[j] = pos_box[p[i]];
swap(pos[i], pos[a[ry[j]]]);
swap(a[rx[j]], a[ry[j]]);
i++;
j++;
}
if (is_sorted())
r = mid;
else
l = mid;
}
return r;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:34:13: warning: unused variable 'steps' [-Wunused-variable]
34 | int steps = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |