#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]);
}
// cout << " after enemy swaps:";
// FOR(i, 0, N-1) cout << " " << ids[i];
// cout << endl;
valSwaps.clear();
FOR(i,0,N-2) {
int val1 = vals[i], val2 = ids[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});
}
// cout << " valSwaps:" << endl;
// for (auto pp : valSwaps) cout << " " << pp.f << ", " << pp.s << endl;
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 (int)valSwaps.size();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |