# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1092714 | triplem5ds | Sorting (IOI15_sorting) | C++14 | 1 ms | 600 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;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
vector<pair<int, int>> ans;
auto check = [&](int prefix) {
ans.clear();
vector<int> vec(N), vis(N, 0), pos(N);
copy(S, S + N, vec.begin());
for (int i = 0; i < prefix; i++) swap(vec[X[i]], vec[Y[i]]);
for (int i = 0; i < N; i++) {
int cur = i;
vis[cur] = 1;
while (!vis[vec[cur]])
{
ans.push_back({ cur,vec[cur] });
cur = vec[cur];
vis[cur] = 1;
}
}
return ans.size() <= prefix;
};
int lo = 0, hi = M;
while (lo < hi)
{
int md = lo + (hi - lo) / 2;
if (check(md)) hi = md;
else lo = md + 1;
}
reverse(ans.begin(), ans.end());
while (ans.size() <= lo) ans.push_back({ 0, 0 });
vector<int> pos(N), vec(N);
for (int i = 0; i < N; i++) {
pos[i] = vec[i] = i;
}
for (int i = lo - 1; i >= 0; --i) {
P[i] = pos[ans[i].first];
Q[i] = pos[ans[i].second];
swap(pos[vec[X[i]]], pos[vec[Y[i]]]);
swap(vec[X[i]], vec[Y[i]]);
}
return lo;
}
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... |