This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "sorting.h"
//#include "grader.cpp"
using namespace std;
const int nn = (int)2e5 + 7;
int x[nn], y[nn], p[nn], q[nn], n, s[nn], m;
int pos[nn], pos1[nn], p1[nn], p2[nn];
void upd(int a, int b) {
swap(p1[a], p1[b]);
p2[p1[a]] = a;
p2[p1[b]] = b;
}
void upd1(int a, int b) {
swap(pos[a], pos[b]);
pos1[pos[a]] = a;
pos1[pos[b]] = b;
}
bool check(int val) {
iota(pos, pos + n, 0);
iota(p1, p1 + n, 0);
for (int i = 0; i < val; i++) {
swap(pos[x[i]], pos[y[i]]);
}
for (int i = 0; i < n; i++) {
p2[p1[i]] = i;
pos1[pos[i]] = i;
}
bool fl = 0;
int cur = 0;
for (int i = val - 1; i >= 0; i--) {
int ppos = pos1[cur];
if (s[cur] != p1[ppos]) {
upd(p2[s[cur]], ppos);
}
cur++;
upd(x[i], y[i]);
upd1(x[i], y[i]);
}
for (int i = 0; i < n; i++) {
int ppos = pos1[i];
if (s[i] != p1[ppos]) return 0;
}
return 1;
}
int asd;
void make(int val) {
iota(pos, pos + n, 0);
iota(p1, p1 + n, 0);
for (int i = 0; i < val; i++) {
swap(pos[x[i]], pos[y[i]]);
}
for (int i = 0; i < n; i++) {
p2[p1[i]] = i;
pos1[pos[i]] = i;
}
bool fl = 0;
int cur = 0;
asd = val - 1;;
for (int i = val - 1; i >= 0; i--) {
int ppos = pos1[cur];
if (s[cur] != p1[ppos]) {
p[asd] = p2[s[cur]];
q[asd--] = ppos;
upd(p2[s[cur]], ppos);
}
cur++;
upd(x[i], y[i]);
upd1(x[i], y[i]);
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N; m = M;
for (int i = 0; i < n; i++)
s[i] = S[i];
for (int i = 0; i < m; i++) {
x[i] = X[i]; y[i] = Y[i];
}
int l = 0;
int r = n + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
if (check(l)) r = l;
make(r);
for (int i = 0; i <= asd; i++) {
P[i] = 0;
Q[i] = 0;
}
for (int i = asd; i < r; i++) {
P[i] = p[i];
Q[i] = q[i];
}
return r;
}
//main() {
// int n;
// cin >> n;
// int S[n];
// for (int i = 0; i < n; i++) {
// cin >> S[i];
// }
// int m;
// cin >> m;
// int X[M], Y[M];
// for (int i = 0; i < m; i++) {
// cin >> X[i] >> Y[i];
// }
// int ans1[M], ans2[M];
// int sz = findSwapPairs(n, S, m, X, Y, ans1, ans2);
// cout << sz << endl;
// for (int i = 0; i < sz; i++) {
// cout << ans1[i] << ' ' << ans2[i] << endl;
// }
//}
Compilation message (stderr)
sorting.cpp: In function 'bool check(int)':
sorting.cpp:34:8: warning: unused variable 'fl' [-Wunused-variable]
bool fl = 0;
^~
sorting.cpp: In function 'void make(int)':
sorting.cpp:64:8: warning: unused variable 'fl' [-Wunused-variable]
bool fl = 0;
^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |