# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
654851 | benjaminkleyn | Sorting (IOI15_sorting) | C++17 | 203 ms | 19324 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 <bits/stdc++.h>
#include "sorting.h"
using namespace std;
typedef long long ll;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
int lo = 0, hi = M;
vector<int> s(N);
vector<int> pos(N);
vector<pair<int,int>> swaps;
while (true)
{
int mid = (lo + hi) / 2;
for (int i = 0; i < N; i++)
s[i] = S[i];
for (int i = 0; i < mid; i++)
swap(s[X[i]], s[Y[i]]);
for (int i = 0; i < N; i++)
pos[s[i]] = i;
swaps.resize(0);
for (int i = 0; i < N; i++)
if (s[i] != i)
{
int x = i, y = pos[i];
swaps.push_back({s[i], i});
swap(s[x], s[y]);
pos[s[x]] = x;
pos[s[y]] = y;
}
if (lo == hi)
break;
if (swaps.size() <= mid)
hi = mid;
else
lo = mid + 1;
}
for (int i = 0; i < N; i++)
pos[S[i]] = i;
for (int i = 0; i < swaps.size(); i++)
{
swap(pos[S[X[i]]], pos[S[Y[i]]]);
swap(S[X[i]], S[Y[i]]);
P[i] = pos[swaps[i].first], Q[i] = pos[swaps[i].second];
swap(pos[S[P[i]]], pos[S[Q[i]]]);
swap(S[P[i]], S[Q[i]]);
}
for (int i = swaps.size(); i < hi; i++)
P[i] = Q[i] = 0;
return hi;
}
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... |