# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
99206 | eriksuenderhauf | Sorting (IOI15_sorting) | C++11 | 748 ms | 30724 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "sorting.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
int n = 0, m = 0;
int x[MAXN], y[MAXN], p[MAXN], q[MAXN], sz[MAXN], ind2[MAXN];
int p2[MAXN], q2[MAXN], par[MAXN], a[MAXN], ind[MAXN];
int qry(int x) {
if (x == par[x]) return x;
return par[x] = qry(par[x]);
}
void join(int x, int y) {
x = qry(x), y = qry(y);
if (x == y) return;
if (sz[x] > sz[y]) swap(x, y);
sz[y] += sz[x];
par[x] = y;
}
bool solve(int cnt) {
for (int i = 0; i < n; i++)
par[i] = i, sz[i] = 1, ind[i] = i;
for (int i = 0; i < cnt; i++) {
swap(a[x[i]], a[y[i]]);
swap(ind[x[i]], ind[y[i]]);
}
for (int i = 0; i < n; i++)
join(i, a[i]);
vii ans;
int cnt2 = 0;
for (int i = 0; i < n; i++) {
if (qry(i) != i)
continue;
cnt2 += sz[i] - 1;
int nx = a[i], pr = i;
while (nx != i) {
ans.pb({ind[i], ind[nx]});
pr = nx;
nx = a[nx];
}
}
while (ans.size() < cnt)
ans.pb({0, 0});
for (int i = 0; i < n; i++)
ind[i] = i, ind2[i] = i;
for (int i = 0; i < cnt; i++) {
swap(ind2[ind[x[i]]], ind2[ind[y[i]]]);
swap(ind[x[i]], ind[y[i]]);
p[i] = ind2[ans[i].fi], q[i] = ind2[ans[i].se];
}
return (ans.size() == cnt);
}
void cp(int cnt) {
for (int i = 0; i < cnt; i++)
p2[i] = p[i], q2[i] = q[i];
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N, m = M;
int lo = 0, hi = n - 1, ans = -1;
for (int i = 0; i < m; i++)
x[i] = X[i], y[i] = Y[i];
while (lo <= hi) {
int mi = (lo + hi) / 2;
for (int i = 0; i < n; i++)
a[i] = S[i];
if (solve(mi)) {
hi = mi - 1, ans = mi;
cp(ans);
} else
lo = mi + 1;
}
for (int i = 0; i < ans; i++)
P[i] = p2[i], Q[i] = q2[i];
return ans;
}
Compilation message (stderr)
# | 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... |