# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1059203 | andrei_iorgulescu | Sorting (IOI15_sorting) | C++14 | 163 ms | 33168 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"
#warning That's not FB, that's my FB
using namespace std;
int n;
bool viz[200005];
vector<int> lols;
vector<vector<int>> cicli;
vector<int> cur;
void dfs(int nod)
{
viz[nod] = true;
cur.push_back(nod);
if (!viz[lols[nod]])
dfs(lols[nod]);
}
int cnt_cyc(vector<int> s)
{
cicli.clear();
lols = s;
memset(viz, 0, sizeof(viz));
for (int i = 0; i < n; i++)
{
if (!viz[i])
{
cur.clear();
dfs(i);
cicli.push_back(cur);
}
}
return cicli.size();
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n = N;
int st = -1, pas = 1 << 17;
while (pas != 0)
{
if (st + pas <= M)
{
vector<int> s(N);
for (int i = 0; i < N; i++)
s[i] = S[i];
for (int i = 0; i < st + pas; i++)
swap(s[X[i]], s[Y[i]]);
if (n - cnt_cyc(s) > st + pas)
st += pas;
}
pas /= 2;
}
st++;
int ans = st;
vector<pair<int,int>> req_swaps;
vector<int> s(N);
for (int i = 0; i < N; i++)
s[i] = S[i];
for (int i = 0; i < st + pas; i++)
swap(s[X[i]], s[Y[i]]);
int useless = cnt_cyc(s);
for (int i = 0; i < useless; i++)
for (int j = (int)cicli[i].size() - 2; j >= 0; j--)
req_swaps.push_back({cicli[i].back(),cicli[i][j]});
vector<int> pos(N);
for (int i = 0; i < N; i++)
pos[S[i]] = i;
for (int i = req_swaps.size(); i < ans; i++)
P[i] = Q[i] = 0;
for (int i = 0; i < req_swaps.size(); i++)
{
swap(S[X[i]],S[Y[i]]);
pos[S[X[i]]] = X[i];
pos[S[Y[i]]] = Y[i];
P[i] = pos[req_swaps[i].first];
Q[i] = pos[req_swaps[i].second];
swap(S[P[i]],S[Q[i]]);
pos[S[P[i]]] = P[i];
pos[S[Q[i]]] = Q[i];
}
return ans;
}
///;gimme 1200 +constructive +greedy (nice one still)
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... |