#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
const int MAXN = 200100;
int ids[MAXN];
int vals[MAXN];
int whereId[MAXN], whereVal[MAXN];
vii valSwaps;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
bool isSorted = true;
FOR(i, 0, N-1) if (S[i] != i) isSorted = false;
if (isSorted) return 0;
FOR(i, 0, M-1) P[i] = Q[i] = 0;
FOR(i, 0, N-1) {
ids[i] = i;
vals[i] = S[i];
whereId[i] = i;
whereVal[S[i]] = i;
}
FOR(i, 0, M-1) {
int a = X[i], b = Y[i];
int id1 = whereId[a], id2 = whereId[b];
swap(whereId[a], whereId[b]);
swap(ids[id1], ids[id2]);
}
valSwaps.clear();
FOR(i,0,N-2) {
int val1 = vals[i], val2 = i;
if (val1 == val2) continue;
int id1 = i, id2 = whereVal[val2];
swap(vals[id1], vals[id2]);
swap(whereVal[val1], whereVal[val2]);
valSwaps.pb({val1, val2});
}
FOR(i, 0, N-1) {
ids[i] = i;
vals[i] = S[i];
whereId[i] = i;
whereVal[S[i]] = i;
}
FOR(i, 0, (int)valSwaps.size()-1) {
int a = X[i], b = Y[i];
int id1 = whereId[a], id2 = whereId[b];
swap(whereId[a], whereId[b]);
swap(ids[id1], ids[id2]);
a = valSwaps[i].f, b = valSwaps[i].s;
id1 = whereVal[a], id2 = whereVal[b];
swap(vals[id1], vals[id2]);
swap(whereVal[a], whereVal[b]);
P[i] = ids[id1];
Q[i] = ids[id2];
}
return M;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |