Submission #102713

#TimeUsernameProblemLanguageResultExecution timeMemory
102713wxh010910Sorting (IOI15_sorting)C++17
100 / 100
224 ms18760 KiB
#include <bits/stdc++.h> #include "sorting.h" using namespace std; int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { vector<bool> visit(N); vector<int> after(N); auto check = [&](int mid) { for (int i = 0; i < N; ++i) { after[i] = S[i]; visit[i] = false; } for (int i = 0; i < mid; ++i) { swap(after[X[i]], after[Y[i]]); } int ans = 0; for (int i = 0; i < N; ++i) { if (!visit[i]) { for (int j = i; ; j = after[j]) { visit[j] = true; if (!visit[after[j]]) { P[ans] = i; Q[ans] = after[j]; ++ans; } else { break; } } } } for (int i = ans; i < mid; ++i) { P[i] = Q[i] = 0; } return ans <= mid; }; int l = 0, r = N; while (l < r) { int mid = (l + r) >> 1; if (check(mid)) { r = mid; } else { l = mid + 1; } } check(r); vector<int> pos(N); vector<int> to(N); for (int i = 0; i < N; ++i) { pos[i] = to[i] = i; } for (int i = r - 1; ~i; --i) { P[i] = to[P[i]]; Q[i] = to[Q[i]]; swap(pos[X[i]], pos[Y[i]]); to[pos[X[i]]] = X[i]; to[pos[Y[i]]] = Y[i]; } return r; }

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:6:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...