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>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
#define round asfhuqfuqwhuqwer
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 200005;
ii round[3 * MAXN];
int val[MAXN], P[6 * MAXN][21], Pn[6 * MAXN][21], lastOf[MAXN];
int lastIdx[MAXN], pos[MAXN], tmp[MAXN], nArr, numRound;
#ifdef Nhoksocqt1
int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[]);
int _S[MAXN], _X[MAXN], _Y[MAXN], _P[MAXN], _Q[MAXN];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("SORTING.inp", "r", stdin);
freopen("SORTING.out", "w", stdout);
int _N, _M;
cin >> _N;
for (int i = 0; i < _N; ++i)
cin >> _S[i];
//for (int i = 0; i < _N; ++i) _S[i] = i; shuffle(_S, _S + _N, rng); for (int i = 0; i < _N; ++i) cout << _S[i] << " \n"[i + 1 == _N];
cin >> _M;
for (int i = 0; i < _M; ++i) {
cin >> _X[i];
//_X[i] = Random(0, _N - 1); cout << _X[i] << " \n"[i + 1 == _M];
}
for (int i = 0; i < _M; ++i) {
cin >> _Y[i];
//_Y[i] = Random(0, _N - 1); cout << _Y[i] << " \n"[i + 1 == _M];
}
int ans = findSwapPairs(_N, _S, _M, _X, _Y, _P, _Q);
cout << ans << '\n';
for (int it = 0; it < ans; ++it)
cout << _P[it] << ' ' << _Q[it] << '\n';
return 0;
}
#endif // Nhoksocqt1
inline int getID(int x) {
return (x > numRound) ? round[x - numRound].fi : round[x].se;
}
bool check(int nr, int _P[], int _Q[]) {
for (int i = 1; i <= nArr; ++i) {
tmp[i] = val[i];
pos[val[i]] = i;
}
swap(pos[tmp[round[1].fi]], pos[tmp[round[1].se]]);
swap(tmp[round[1].fi], tmp[round[1].se]);
int cntNow(1);
for (int i = 1; i <= nArr; ++i) {
int from(lastOf[i]);
for (int j = 20; j >= 0; --j) {
int now(Pn[from][j]);
if(now <= numRound && now > nr || now > numRound && now - numRound > nr)
from = now;
}
if(from <= numRound && from > nr || from > numRound && from - numRound > nr)
from = Pn[from][0];
//cout << ',' << from << ' ';
for (int j = 20; j >= 0; --j) {
int now(P[from][j]);
if(!now)
continue;
//cout << now << ' ' << cntNow << '.';
if(now <= numRound && now > cntNow || now > numRound && now - numRound > cntNow)
from = now;
}
//cout << nr << ' ' << i << ' ' << tmp[getID(from)] << ' ' << lastOf[i] << ' ' << from << ' ' << getID(from) << '\n';
bool checkCond = (from <= numRound && from > cntNow || from > numRound && from - numRound > cntNow);
if(checkCond && tmp[getID(from)] == i || !checkCond && tmp[i] == i)
continue;
if(cntNow > nr)
return false;
int x(pos[i]), y;
if(checkCond) {
x = pos[i], y = getID(from);
} else {
x = pos[i], y = i;
}
_P[cntNow - 1] = x - 1, _Q[cntNow - 1] = y - 1;
swap(pos[tmp[x]], pos[tmp[y]]);
swap(tmp[x], tmp[y]);
//cout << '.' << x << ' ' << y << '\n';
if(++cntNow <= nr) {
swap(pos[tmp[round[cntNow].fi]], pos[tmp[round[cntNow].se]]);
swap(tmp[round[cntNow].fi], tmp[round[cntNow].se]);
}
}
for (int i = cntNow - 1; i < nr; ++i)
_P[i] = _Q[i] = 0;
for (int i = cntNow + 1; i <= nr; ++i)
swap(tmp[round[i].fi], tmp[round[i].se]);
/*cout << nr << ": ";
for (int i = 1; i <= nArr; ++i)
cout << tmp[i] << " \n"[i == nArr];*/
return true;
}
int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[]) {
nArr = _N, numRound = _M;
bool check0(1);
for (int i = 1; i <= nArr; ++i) {
val[i] = _S[i - 1] + 1;
check0 &= (val[i] == i);
}
if(check0)
return 0;
for (int i = 1; i <= numRound; ++i) {
round[i] = {_X[i - 1] + 1, _Y[i - 1] + 1};
P[i][0] = lastIdx[round[i].se];
P[i + numRound][0] = lastIdx[round[i].fi];
lastIdx[round[i].fi] = i;
lastIdx[round[i].se] = i + numRound;
}
for (int i = 1; i <= nArr; ++i)
lastIdx[i] = 0;
for (int i = 1; i <= numRound; ++i) {
Pn[i][0] = lastIdx[round[i].fi];
Pn[i + numRound][0] = lastIdx[round[i].se];
lastIdx[round[i].fi] = i;
lastIdx[round[i].se] = i + numRound;
lastOf[round[i].fi] = i;
lastOf[round[i].se] = i + numRound;
}
for (int j = 1; j <= 20; ++j) {
for (int i = 1; i <= 2 * numRound; ++i) {
P[i][j] = P[P[i][j - 1]][j - 1];
Pn[i][j] = Pn[Pn[i][j - 1]][j - 1];
}
}
int l(1), r(numRound - 1), answer(r + 1);
while(l <= r) {
int mid = (l + r) >> 1;
if(check(mid, _P, _Q)) {
answer = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
check(answer, _P, _Q);
return answer;
}
Compilation message (stderr)
sorting.cpp: In function 'bool check(int, int*, int*)':
sorting.cpp:86:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
86 | if(now <= numRound && now > nr || now > numRound && now - numRound > nr)
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
sorting.cpp:90:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
90 | if(from <= numRound && from > nr || from > numRound && from - numRound > nr)
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
sorting.cpp:100:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
100 | if(now <= numRound && now > cntNow || now > numRound && now - numRound > cntNow)
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
sorting.cpp:105:44: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
105 | bool checkCond = (from <= numRound && from > cntNow || from > numRound && from - numRound > cntNow);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
sorting.cpp:106:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
106 | if(checkCond && tmp[getID(from)] == i || !checkCond && tmp[i] == i)
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |