Submission #544244

#TimeUsernameProblemLanguageResultExecution timeMemory
544244timreizinSorting (IOI15_sorting)C++17
100 / 100
141 ms23896 KiB
#include "sorting.h" #include <vector> #include <algorithm> #include <iostream> using namespace std; bool check(int m, vector<int> s, vector<pair<int, int>> &swaps) { for_each(swaps.begin(), swaps.begin() + m, [&s](auto i){ swap(s[i.first], s[i.second]); }); int res = 0; vector<int> pos(s.size()); for (int i = 0; i < s.size(); ++i) pos[s[i]] = i; for (int i = 0; i < s.size(); ++i) { if (s[i] == i) continue; ++res; swap(s[i], s[pos[i]]); swap(pos[i], pos[s[pos[i]]]); } return res <= m; } int findSwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[]) { vector<int> s(n); for (int i = 0; i < n; ++i) s[i] = S[i]; vector<pair<int, int>> swaps(m); for (int i = 0; i < m; ++i) { swaps[i].first = X[i]; swaps[i].second = Y[i]; } int l = 0, r = m; while (l < r) { int m = (l + r) >> 1; if (check(m, s, swaps)) r = m; else l = m + 1; } vector<int> finish = s; for_each(swaps.begin(), swaps.begin() + l, [&s = finish](auto i){ swap(s[i.first], s[i.second]); }); vector<int> pos(n), finPos(n); for (int i = 0; i < n; ++i) { pos[s[i]] = i; finPos[finish[i]] = i; } for (int i = 0, j = 0; j < l; ++i) { if (i == n) { P[l - 1] = 0; Q[l - 1] = 0; break; } if (finish[i] == i) continue; swap(pos[s[swaps[j].first]], pos[s[swaps[j].second]]); swap(s[swaps[j].first], s[swaps[j].second]); int a = finish[i], b = i; swap(finish[i], finish[finPos[i]]); swap(finPos[i], finPos[finish[finPos[i]]]); P[j] = pos[a]; Q[j] = pos[b]; swap(s[pos[a]], s[pos[b]]); swap(pos[a], pos[b]); ++j; } return l; }

Compilation message (stderr)

sorting.cpp: In function 'bool check(int, std::vector<int>, std::vector<std::pair<int, int> >&)':
sorting.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < s.size(); ++i) pos[s[i]] = i;
      |                     ~~^~~~~~~~~~
sorting.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; i < s.size(); ++i)
      |                     ~~^~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:37:13: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   37 |         int m = (l + r) >> 1;
      |             ^
sorting.cpp:24:39: note: shadowed declaration is here
   24 | 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...