# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
46336 | jun6873 | Sorting (IOI15_sorting) | C++11 | 634 ms | 29464 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.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pint;
#define x first
#define y second
int n, *s;
int m, *x, *y, *p, *q;
const int maxn = 200004;
bool vis[maxn];
int now[maxn], rev[maxn];
int dfs(int *nxt, vector<pint>& rec, int k) {
if (vis[k]) return k; vis[k] = 1;
int l = dfs(nxt, rec, nxt[k]);
if (k!=l) rec.emplace_back(k, l);
return l;
}
bool possible(int k) {
copy(s, s+n, now);
for (int i=0; i<k; i++) swap(now[x[i]], now[y[i]]);
vector<pint> pr;
memset(vis, 0, sizeof(vis));
for (int i=0; i<n; i++) if (!vis[i]) dfs(now, pr, i);
if (pr.size() > k) return false;
iota(now, now+n, 0);
iota(rev, rev+n, 0);
for (int i=k-1; ~i; i--) {
if (pr.size() <= i) {
p[i] = q[i] = 0;
} else {
p[i] = rev[pr[i].x];
q[i] = rev[pr[i].y];
}
swap(rev[now[p[i]]], rev[now[q[i]]]); swap(now[p[i]], now[q[i]]);
swap(rev[now[x[i]]], rev[now[y[i]]]); swap(now[x[i]], now[y[i]]);
}
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N; s = S; m = M; x = X; y = Y; p = P; q = Q;
int l = -1, r = m+1;
while (l+1<r) {
int m = (l+r)/2;
if (possible(m)) r = m;
else l = m;
}
return r;
}
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... |