# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
293965 | arayi | Sorting (IOI15_sorting) | C++17 | 2 ms | 640 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;
const int N = 2e5 + 30;
int n, m;
int a[N], ind[N], b[N], x[N], y[N];
int col[N];
void dfs(int v)
{
col[v] = 1;
if(!col[b[v]]) dfs(b[v]);
}
int cnt()
{
int ret = 0;
for (int i = 0; i < n; i++)
if(!col[i]) ret++, dfs(i);
for (int i = 0; i < n; i++) col[i] = 0;
return n - ret;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n = N;
for (int i = 0; i < n; i++) a[i] = S[i], ind[a[i]] = i;
for (int i = 0; i < M; i++) x[i] = X[i], y[i] = Y[i];
int l = 0, r = M, ans = M;
while(l <= r)
{
int mid = (l + r) / 2;
for (int i = 0; i < n; i++) b[i] = a[i];
for (int i = 0; i < mid; i++) swap(b[x[i]], b[y[i]]);
int sm = cnt();
if(sm <= mid) ans = mid, r = mid - 1;
else l = mid + 1;
}
m = ans;
for (int i = 0; i < m; i++)
{
if(x[i] == y[i]) continue;
col[x[i]]++, col[y[i]]++;
}
queue <int> q;
for (int i = 0; i < n; i++)
{
if(!col[i]) q.push(i);
}
for (int i = 0; i < m; i++)
{
swap(a[x[i]], a[y[i]]);
if(x[i] != y[i])
{
col[x[i]]--, col[y[i]]--;
if(!col[x[i]]) q.push(x[i]);
if(!col[y[i]]) q.push(y[i]);
ind[a[x[i]]] = x[i];
ind[a[y[i]]] = y[i];
}
while(!q.empty() && q.front() == a[q.front()]) q.pop();
if(q.empty()) P[i] = Q[i] = 0;
else
{
int i1 = q.front();
//cout << i1 << endl;
P[i] = i1;
Q[i] = ind[i1];
swap(a[P[i]], a[Q[i]]);
ind[a[P[i]]] = P[i];
ind[a[Q[i]]] = Q[i];
q.pop();
}
}
return m;
}
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... |