#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[]) {
vector<int> targetArr(n);
vector<int> revInd(n);
for(int i = 0; i < n; i++) {
targetArr[i] = i;
}
for(int i = m-1; i >= 0; i--) {
swap(targetArr[X[i]], targetArr[Y[i]]);
}
for(int i = 0; i < n; i++) {
revInd[targetArr[i]] = i;
}
set<int> notGood; // Indices of places where no match
for(int i = 0; i < n; i++) {
if(S[i] != targetArr[i]) notGood.insert(i);
}
auto swapTwo = [&](int i, int a, int b) {
swap(S[a], S[b]);
P[i] = a; Q[i] = b;
};
for(int i = 0; i < m; i++) {
if(X[i] != Y[i]) {
int x = (int)notGood.count(X[i]);
int y = (int)notGood.count(Y[i]);
swap(revInd[targetArr[X[i]]], revInd[targetArr[Y[i]]]);
swap(targetArr[X[i]], targetArr[Y[i]]);
swap(S[X[i]], S[Y[i]]);
notGood.erase(X[i]);
notGood.erase(Y[i]);
if(x) notGood.insert(Y[i]);
if(y) notGood.insert(X[i]);
}
if(notGood.size() == 0) {
swapTwo(i, 0, 0);
continue;
}
int a = *(notGood.begin());
int b = revInd[S[a]];
swapTwo(i, a, b);
assert(S[b] == targetArr[b]);
if(S[a] == targetArr[a]) notGood.erase(a);
if(S[b] == targetArr[b]) notGood.erase(b);
}
return m;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |