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 <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define vi vector<int>
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef short inth;
const int K = 2e5 + 7;
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 17;
int m, n;
pii query[K], ans[K];
pii temp[K];
int pos[K];
bool check(int t, int S[]) {
int out[n], in[n];
for (int i = 0; i < n; ++i)
out[i] = in[i] = S[i];
for (int id = 0; id < t; ++id) {
int i = query[id].fi, j = query[id].se;
swap(out[i], out[j]);
}
bool us[n];
memset(us, false, sizeof us);
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (us[i])
continue;
vi ord;
int v = out[i];
while (v != i) {
ord.pb(v);
us[v] = true;
v = out[v];
}
ord.pb(v);
us[v] = true;
for (int j = sz(ord) - 1; j > 0; --j)
temp[cnt++] = mp(ord[0], ord[j]);
}
if (cnt > t)
return false;
for (int i = 0; i < n; ++i)
pos[in[i]] = i;
for (int id = 0; id < cnt; ++id) {
int i = query[id].fi, j = query[id].se;
swap(in[i], in[j]);
pos[in[i]] = i;
pos[in[j]] = j;
i = pos[temp[id].fi]; j = pos[temp[id].se];
ans[id] = mp(i, j);
swap(in[i], in[j]);
pos[in[i]] = i;
pos[in[j]] = j;
}
while (cnt < t)
ans[cnt++] = mp(0, 0);
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = -1, r = M;
m = M; n = N;
for (int i = 0; i < m; ++i)
query[i] = mp(X[i], Y[i]);
while (r - l > 1) {
int m = (l + r) >> 1;
if (check(m, S)) {
r = m;
} else {
l = m;
}
}
check(r, S);
for (int i = 0; i < r; ++i) {
P[i] = ans[i].fi;
Q[i] = ans[i].se;
}
return r;
}
Compilation message (stderr)
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:119:13: warning: declaration of 'm' shadows a global declaration [-Wshadow]
int m = (l + r) >> 1;
^
sorting.cpp:45:5: note: shadowed declaration is here
int m, n;
^
# | 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... |