Submission #129522

#TimeUsernameProblemLanguageResultExecution timeMemory
129522johuthaSorting (IOI15_sorting)C++14
100 / 100
186 ms21988 KiB
#include "sorting.h" #include <vector> #include <iostream> using namespace std; int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) { vector<int> perm(n); bool bc = true; for (int i = 0; i < n; i++) { perm[i] = S[i]; if (perm[i] != i) bc = false; } if (bc) return 0; vector<int> b = perm; vector<int> beg = perm; for (int i = 0; i < n; i++) { swap(perm[X[i]], perm[Y[i]]); } vector<int> end = perm; int l = 0; int r = n; while (r - l > 1) { int m = (l + r)/2; vector<int> mv = beg; for (int i = l; i < m; i++) { swap(mv[X[i]], mv[Y[i]]); } vector<bool> v(n, false); int ctr = 0; for (int i = 0; i < n; i++) { if (v[i]) continue; v[i] = true; int cc = 0; int cr = i; while (mv[cr] != i) { cc++; cr = mv[cr]; v[cr] = true; } ctr += cc; } if (ctr <= m) { r = m; end = mv; } else { l = m; beg = mv; } } vector<pair<int,int>> swp; perm = b; for (int i = 0; i < n; i++) { while (end[i] != i) { swp.push_back({end[i], end[end[i]]}); swap(end[i], end[end[i]]); } } vector<int> revind(n); for (int i = 0; i < n; i++) { revind[perm[i]] = i; } for (int i = 0; i < r; i++) { swap(revind[perm[X[i]]], revind[perm[Y[i]]]); swap(perm[X[i]], perm[Y[i]]); if (i < swp.size()) { Q[i] = revind[swp[i].first]; P[i] = revind[swp[i].second]; swap(revind[swp[i].first], revind[swp[i].second]); swap(perm[Q[i]], perm[P[i]]); } else { Q[i] = 0; P[i] = 0; } } return r; }

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:85:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i < swp.size())
       ~~^~~~~~~~~~~~
sorting.cpp:7: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...