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>
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; --i)
#define rrep2(i, n, t) for(ll i = ll(n)-1; i >= ll(t); --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
int ok = n - 1, ng = -1;
auto f = [&](int i) -> bool {
vi to(n);
iota(all(to), 0);
vi rev(n);
iota(all(rev), 0);
rep(j, i) {
swap(to[rev[x[j]]], to[rev[y[j]]]);
swap(rev[x[j]], rev[y[j]]);
}
vvi ls;
vb seen(n);
rep(j, n) {
if (seen[j]) continue;
ls.pb({});
int now = j;
while (true) {
seen[now] = true;
ls.back().pb(now);
int nx = s[rev[now]];
if (seen[nx]) break;
now = nx;
}
}
return n - SZ(ls) <= i;
};
while (ok - ng > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
int i = ok;
vi to(n);
iota(all(to), 0);
vi rev(n);
iota(all(rev), 0);
rep(j, i) {
swap(to[rev[x[j]]], to[rev[y[j]]]);
swap(rev[x[j]], rev[y[j]]);
}
vvi ls;
vb seen(n);
rep(j, n) {
if (seen[j]) continue;
ls.pb({});
int now = j;
while (true) {
seen[now] = true;
ls.back().pb(now);
int nx = s[rev[now]];
if (seen[nx]) break;
now = nx;
}
}
vp ev;
for (const vi &v: ls) {
rep2(j, 1, SZ(v)) ev.eb(v[0], v[j]);
}
rep(j, i) {
swap(rev[to[x[j]]], rev[to[y[j]]]);
swap(to[x[j]], to[y[j]]);
if (j < SZ(ev)) {
p[j] = rev[ev[j].first];
q[j] = rev[ev[j].second];
} else {
p[j] = q[j] = 0;
}
}
return i;
}
Compilation message (stderr)
sorting.cpp: In lambda function:
sorting.cpp:60:23: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
60 | int now = j;
| ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:90:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
90 | int now = j;
| ^
sorting.cpp:44:39: warning: unused parameter 'm' [-Wunused-parameter]
44 | int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
| ~~~~^
# | 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... |