# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
778393 | danikoynov | Sorting (IOI15_sorting) | C++14 | 153 ms | 18340 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;
const int maxn = 2e5 + 10;
int n, arr[maxn], used[maxn];
vector < pair < int, int > > edges;
int count_steps()
{
for (int i = 0; i < n; i ++)
used[i] = 0;
int ans = 0;
for (int i = 0; i < n; i ++)
{
if (used[i])
continue;
int v = arr[i], cnt = 0;
while(v != i)
{
used[v] = 1;
cnt ++;
v = arr[v];
}
used[v] = 1;
cnt ++;
ans = ans + (cnt - 1);
}
return ans;
}
void build_edges()
{
for (int i = 0; i < n; i ++)
used[i] = 0;
for (int i = 0; i < n; i ++)
{
if (used[i])
continue;
vector < int > ord;
int v = arr[i];
while(v != i)
{
ord.push_back(v);
used[v] = 1;
v = arr[v];
}
ord.push_back(i);
used[i] = 1;
for (int i = 0; i < ord.size() - 1; i ++)
edges.push_back({arr[ord[i]], arr[ord[i + 1]]});
}
}
int pos[maxn];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n = N;
bool sorted = true;
for (int i = 0; i < N; i ++)
if (S[i] != i)
sorted = false;
if (sorted)
return 0;
for (int i = 0; i < N; i ++)
arr[i] = S[i];
int l = 0, r = M - 1;
while(l <= r)
{
int m = (l + r) / 2;
for (int i = 0; i < N; i ++)
arr[i] = S[i];
for (int i = 0; i <= m; i ++)
swap(arr[X[i]], arr[Y[i]]);
//cout << "check " << m << " " << count_steps() << endl;
if (count_steps() <= m + 1)
r = m - 1;
else
l = m + 1;
}
int pivot = l;
for (int i = 0; i < N; i ++)
arr[i] = S[i];
for (int i = 0; i <= pivot; i ++)
swap(arr[X[i]], arr[Y[i]]);
/**for (int i = 0; i < M; i ++)
{
swap(arr[X[i]], arr[Y[i]]);
int steps = count_steps();
if (steps <= i + 1)
{
pivot = i;
break;
}
}
cout << pivot << endl;*/
///assert(pivot != -1);
///cout << pivot << endl;
///exit(0);
build_edges();
for (int i = 0; i < n; i ++)
pos[S[i]] = i, arr[i] = S[i];
for (int i = 0; i < edges.size(); i ++)
{
swap(arr[X[i]], arr[Y[i]]);
swap(pos[arr[X[i]]], pos[arr[Y[i]]]);
///cout << edges[i].first << " : " << edges[i].second << endl;
P[i] = pos[edges[i].first];
Q[i] = pos[edges[i].second];
swap(arr[Q[i]], arr[P[i]]);
swap(pos[edges[i].first], pos[edges[i].second]);
///cout << id1 << " : " << id2 << endl;
///swap(pos[X[i]], pos[Y[i]]);
}
int left = pivot - edges.size() + 1;
if (left % 2 == 0)
{
for (int i = edges.size(); i <= pivot; i ++)
{
P[i] = 0;
Q[i] = 1;
}
}
else
{
for (int i = edges.size(); i < pivot; i ++)
{
P[i] = 0;
Q[i] = 1;
}
P[pivot] = Q[pivot] = 0;
}
/**for (int i = 0; i <= pivot; i ++)
{ swap(S[X[i]], S[Y[i]]);
swap(S[P[i]], S[Q[i]]);
}*/
/**for (int i = 0; i < N; i ++)
cout << S[i] << " ";
cout << endl;*/
///cout << "stop " << edges.size() << " " << pivot << endl;
return pivot + 1;
}
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... |