# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
752783 | 2023-06-03T20:53:11 Z | jampm | Sorting (IOI15_sorting) | C++17 | 0 ms | 0 KB |
#include "sorting.h" #include <bits/stdc++.h> using namespace std; const int LOGN = 30; int count_cycles(vector<int> & v, int n = 0) { n = (int)v.size(); int ans = 0; vector<bool> vis(n, false); for (int i = 0; i < n; i++) { if (vis[i]) continue; ans++; for (int j = v[i]; i != j; j = v[j]) vis[j] = true; } return ans; } int findswappairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) { int ans = 0; for (int k = logn; k >= 0; k--) { if (ans + (1<<k) > m) continue; vector<int> v(n); for (int i = 0; i < n; i++) v[i] = s[i]; for (int i = 0; i < ans + (1<<k); i++) { swap(v[x[i]], v[y[i]]); } if (count_cycles(v) + ans + (1<<k) < n) ans += (1<<k); } ans++; vector<int> v(n); for (int i = 0; i < n; i++) v[i] = s[i]; for (int i = 0; i < ans; i++) { swap(v[x[i]], v[y[i]]); } vector<bool> vis(n, false); int cnt = 0; for (int i = 0; i < n; i++) { if (vis[i]) continue; p[cnt] = i, q[cnt] = vis[i]; cnt++; for (int j = v[i]; i != j; j = v[j]) { p[cnt] = j, q[cnt] = vis[j]; cnt++; } } return ans; }