#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;
int le = 1, ri = M;
while (le < ri) {
int mid = (le+ri)/2;
FOR(i, 0, mid-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, mid-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];
}
if ((int)valSwaps.size() <= mid) ri = mid;
else le = mid+1;
}
FOR(i, 0, le-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, le-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 le;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
300 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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
584 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
440 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
500 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
440 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
500 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
282 ms |
21464 KB |
Output is correct |
16 |
Correct |
349 ms |
21956 KB |
Output is correct |
17 |
Correct |
472 ms |
23112 KB |
Output is correct |
18 |
Correct |
46 ms |
13380 KB |
Output is correct |
19 |
Correct |
301 ms |
22356 KB |
Output is correct |
20 |
Correct |
302 ms |
22964 KB |
Output is correct |
21 |
Correct |
332 ms |
22964 KB |
Output is correct |
22 |
Correct |
288 ms |
18548 KB |
Output is correct |
23 |
Correct |
312 ms |
24172 KB |
Output is correct |
24 |
Correct |
483 ms |
23588 KB |
Output is correct |
25 |
Correct |
458 ms |
23308 KB |
Output is correct |
26 |
Correct |
322 ms |
22464 KB |
Output is correct |
27 |
Correct |
250 ms |
21624 KB |
Output is correct |
28 |
Correct |
421 ms |
23460 KB |
Output is correct |
29 |
Correct |
413 ms |
22852 KB |
Output is correct |
30 |
Correct |
203 ms |
20920 KB |
Output is correct |
31 |
Correct |
441 ms |
23452 KB |
Output is correct |
32 |
Correct |
312 ms |
23216 KB |
Output is correct |
33 |
Correct |
535 ms |
23676 KB |
Output is correct |
34 |
Correct |
384 ms |
23624 KB |
Output is correct |
35 |
Correct |
295 ms |
21940 KB |
Output is correct |
36 |
Correct |
79 ms |
19740 KB |
Output is correct |
37 |
Correct |
485 ms |
24320 KB |
Output is correct |
38 |
Correct |
442 ms |
23316 KB |
Output is correct |
39 |
Correct |
459 ms |
23576 KB |
Output is correct |