# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544244 | timreizin | Sorting (IOI15_sorting) | C++17 | 141 ms | 23896 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 <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool check(int m, vector<int> s, vector<pair<int, int>> &swaps)
{
for_each(swaps.begin(), swaps.begin() + m, [&s](auto i){ swap(s[i.first], s[i.second]); });
int res = 0;
vector<int> pos(s.size());
for (int i = 0; i < s.size(); ++i) pos[s[i]] = i;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == i) continue;
++res;
swap(s[i], s[pos[i]]);
swap(pos[i], pos[s[pos[i]]]);
}
return res <= m;
}
int findSwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[])
{
vector<int> s(n);
for (int i = 0; i < n; ++i) s[i] = S[i];
vector<pair<int, int>> swaps(m);
for (int i = 0; i < m; ++i)
{
swaps[i].first = X[i];
swaps[i].second = Y[i];
}
int l = 0, r = m;
while (l < r)
{
int m = (l + r) >> 1;
if (check(m, s, swaps)) r = m;
else l = m + 1;
}
vector<int> finish = s;
for_each(swaps.begin(), swaps.begin() + l, [&s = finish](auto i){ swap(s[i.first], s[i.second]); });
vector<int> pos(n), finPos(n);
for (int i = 0; i < n; ++i)
{
pos[s[i]] = i;
finPos[finish[i]] = i;
}
for (int i = 0, j = 0; j < l; ++i)
{
if (i == n)
{
P[l - 1] = 0;
Q[l - 1] = 0;
break;
}
if (finish[i] == i) continue;
swap(pos[s[swaps[j].first]], pos[s[swaps[j].second]]);
swap(s[swaps[j].first], s[swaps[j].second]);
int a = finish[i], b = i;
swap(finish[i], finish[finPos[i]]);
swap(finPos[i], finPos[finish[finPos[i]]]);
P[j] = pos[a];
Q[j] = pos[b];
swap(s[pos[a]], s[pos[b]]);
swap(pos[a], pos[b]);
++j;
}
return l;
}
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... |