#include <iostream>
#include "sorting.h"
#include <vector>
using namespace std;
int const NMAX = 5e5;
bool canSort(int n, int s[], int m, int x[], int y[]) {
int aux[1 + NMAX], cycle = 0;
int visit[1 + NMAX];
for(int i = 0;i < n;i++) {
aux[i] = s[i];
visit[i] = false;
}
for(int i = 0;i < m;i++) {
swap(aux[x[i]], aux[y[i]]);
}
for(int i = 0;i < n;i++) {
int index = i;
if(!visit[index]) {
cycle++;
while(!visit[index]) {
visit[index] = true;
index = aux[index];
}
}
}
return (m >= n - cycle);
}
int cautbinAns(int from, int to, int n, int s[], int x[], int y[]) {
if(from >= to) {
return from;
} else {
int mid = (from + to) / 2;
if(canSort(n, s, mid, x, y)) {
return cautbinAns(from, mid, n, s, x, y);
} else {
return cautbinAns(mid+1, to, n, s, x, y);
}
}
}
int arr[1 + NMAX];
int arrPos[1 + NMAX];
vector <pair <int, int>> swaps;
void buildSwap(int n, int s[], int m, int x[], int y[]) {
int aux[1 + NMAX];
int visit[1 + NMAX];
for(int i = 0;i < n;i++) {
aux[i] = s[i];
visit[i] = false;
}
for(int i = 0;i < m;i++) {
swap(aux[x[i]], aux[y[i]]);
}
for(int i = 0;i < n;i++) {
//cerr << aux[i] << ' ';
int index = i, to;
if(!visit[index]) {
while(!visit[index]) {
visit[index] = true;
to = aux[index];
if(!visit[to]) {
swaps.push_back({index, to});
}
//cerr << index << ' ' << aux[index] << '\n';
index = to;
}
}
}
//cerr << '\n';
}
void buildArr(int n, int s[]) {
for(int i = 0;i < n;i++) {
arr[i] = s[i];
arrPos[arr[i]] = i;
}
}
void printArr(int n) {
for(int i = 0;i < n;i++) {
cerr << arr[i] << ' ';
}
cerr << "\n";
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
int ans = cautbinAns(0, m, n, s, x, y), fix = 0;
buildSwap(n, s, ans, x, y);
buildArr(n, s);
int ind = 0;
for(int i = 0;i < ans;i++) {
int a = x[i], b = y[i];
swap(arrPos[arr[a]], arrPos[arr[b]]);
swap(arr[a], arr[b]);
if(ind < swaps.size()) {
p[i] = arrPos[swaps[ind].first];
q[i] = arrPos[swaps[ind].second];
//cerr << swaps[ind].first << ' ' << swaps[ind].second << " : " << p[i] << ' ' << q[i] << "\n";
swap(arrPos[arr[p[i]]], arrPos[arr[q[i]]]);
swap(arr[p[i]], arr[q[i]]);
ind++;
} else {
p[i] = q[i] = 0;
}
//printArr(n);
}
return ans;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:102:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if(ind < swaps.size()) {
| ~~~~^~~~~~~~~~~~~~
sorting.cpp:94:43: warning: unused variable 'fix' [-Wunused-variable]
94 | int ans = cautbinAns(0, m, n, s, x, y), fix = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6400 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6336 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6236 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6232 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6400 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6336 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6232 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
3 ms |
6236 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
2 ms |
6488 KB |
Output is correct |
22 |
Correct |
3 ms |
6424 KB |
Output is correct |
23 |
Correct |
3 ms |
6456 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6412 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6488 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6420 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6348 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6416 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6412 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6488 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6420 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6348 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6416 KB |
Output is correct |
14 |
Correct |
2 ms |
6488 KB |
Output is correct |
15 |
Correct |
102 ms |
24516 KB |
Output is correct |
16 |
Correct |
107 ms |
25024 KB |
Output is correct |
17 |
Correct |
116 ms |
26052 KB |
Output is correct |
18 |
Correct |
44 ms |
20056 KB |
Output is correct |
19 |
Correct |
94 ms |
25540 KB |
Output is correct |
20 |
Correct |
97 ms |
24264 KB |
Output is correct |
21 |
Correct |
94 ms |
26188 KB |
Output is correct |
22 |
Correct |
94 ms |
21444 KB |
Output is correct |
23 |
Correct |
113 ms |
26660 KB |
Output is correct |
24 |
Correct |
116 ms |
26320 KB |
Output is correct |
25 |
Correct |
113 ms |
26476 KB |
Output is correct |
26 |
Correct |
97 ms |
26056 KB |
Output is correct |
27 |
Correct |
85 ms |
23496 KB |
Output is correct |
28 |
Correct |
117 ms |
26392 KB |
Output is correct |
29 |
Correct |
107 ms |
25636 KB |
Output is correct |
30 |
Correct |
66 ms |
22476 KB |
Output is correct |
31 |
Correct |
108 ms |
26052 KB |
Output is correct |
32 |
Correct |
109 ms |
25796 KB |
Output is correct |
33 |
Correct |
114 ms |
26560 KB |
Output is correct |
34 |
Correct |
115 ms |
26492 KB |
Output is correct |
35 |
Correct |
98 ms |
23700 KB |
Output is correct |
36 |
Correct |
41 ms |
21076 KB |
Output is correct |
37 |
Correct |
118 ms |
26564 KB |
Output is correct |
38 |
Correct |
116 ms |
25024 KB |
Output is correct |
39 |
Correct |
114 ms |
24532 KB |
Output is correct |