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 "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#endif // BALBIT
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int )((x).size())
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
//signed main(){bug(1,2);}
const int maxn = 2e5+5;
bool seen[maxn];
int where[maxn];
int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
// int IT = 0;
{
int l = 0, r = M;
while (l!=r) {
int mid = (l+r)/2;
vector<int> ts(n);
REP(i,n) ts[i] = S[i];
for (int j = 0; j<mid; ++j) {
swap(ts[X[j]], ts[Y[j]]);
}
memset(seen, 0, sizeof seen);
int tt = n;
REP(i,n) {
if (!seen[i]) {
--tt;
for (int j = i; ; j = ts[j]) {
seen[j] = 1;
if (ts[j] == i) break;
}
}
}
bug(mid, tt, l, r);
if (tt <= mid) {
r = mid;
}else{
l = mid+1;
}
}
bug(l);
vector<int> ts(n);
REP(i,n) ts[i] = S[i];
for (int j = 0; j<l; ++j) {
swap(ts[X[j]], ts[Y[j]]);
}
REP(i,n) bug(ts[i]);
int IT = 0;
REP(i,n) where[S[i]] = i;
for (int i = 0; i<n; ++i) {
while (ts[i] != i) {
bug(i, ts[i]);
{
swap(where[S[X[IT]]], where[S[Y[IT]]]);
swap(S[X[IT]], S[Y[IT]]);
}
int p1 = where[ts[i]], p2 = where[ts[ts[i]]];
P[IT] = p1; Q[IT] = p2;
++IT;
{
swap(where[S[p1]], where[S[p2]]);
swap(S[p1], S[p2]);
}
swap(ts[i], ts[ts[i]]);
bug(i, ts[i]);
}
}
while (IT != l) {
Q[IT] = P[IT] = 0;
++IT;
}
return l;
}
}
# | 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... |