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"
using namespace std;
typedef pair<int, int> pii;
vector<int> perm;
vector<pii> alice, bob;
bool check(int t) {
vector<int> tmp(perm);
for (int i = 0; i < t; i++) {
swap(tmp[alice[i].first], tmp[alice[i].second]);
}
bob.clear();
for (int i = 0; i < tmp.size(); i++) {
while (tmp[i] != i) {
bob.emplace_back(tmp[i], tmp[tmp[i]]);
swap(tmp[i], tmp[tmp[i]]);
}
}
while (bob.size() < t)
bob.emplace_back(0, 0);
return bob.size() == t;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
perm.assign(S, S + N);
for (int i = 0; i < M; i++) {
alice.emplace_back(X[i], Y[i]);
}
int L = -1, R = M;
while (R - L > 1) {
int mid = (L + R) >> 1;
(check(mid) ? R : L) = mid;
}
check(R);
vector<int> inv(N);
for (int i = 0; i < N; i++) {
inv[perm[i]] = i;
}
const auto replc = [&] (int i, int j) {
swap(perm[i], perm[j]);
inv[perm[i]] = i;
inv[perm[j]] = j;
};
for (int i = 0; i < R; i++) {
replc(X[i], Y[i]);
P[i] = inv[bob[i].first];
Q[i] = inv[bob[i].second];
replc(P[i], Q[i]);
}
return R;
}
Compilation message (stderr)
sorting.cpp: In function 'bool check(int)':
sorting.cpp:16:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0; i < tmp.size(); i++) {
| ~~^~~~~~~~~~~~
sorting.cpp:22:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
22 | while (bob.size() < t)
| ~~~~~~~~~~~^~~
sorting.cpp:24:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | return bob.size() == t;
| ~~~~~~~~~~~^~~~
# | 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... |