Submission #302727

#TimeUsernameProblemLanguageResultExecution timeMemory
302727square1001Sorting (IOI15_sorting)C++14
74 / 100
1076 ms10056 KiB
#include "sorting.h" #include <vector> #include <iostream> #include <algorithm> using namespace std; int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { fill(P, P + M, 0); fill(Q, Q + M, 0); if(is_sorted(S, S + N)) { return 0; } int pl = 0, pr = M; while(pr - pl > 1) { int pm = (pl + pr) >> 1; vector<int> T(S, S + N); for(int i = 0; i < pm; ++i) { swap(T[X[i]], T[Y[i]]); } vector<bool> vis(N, false); int cycles = 0; for(int i = 0; i < N; ++i) { if(vis[i]) continue; int pos = i; ++cycles; while(!vis[pos]) { vis[pos] = true; pos = T[pos]; } } int steps = N - cycles; if(pm >= steps) pr = pm; else pl = pm; } int L = pr; for(int i = 0; i < L; ++i) { swap(S[X[i]], S[Y[i]]); } vector<bool> vis2(N, false); int cl = 0; for(int i = 0; i < N; ++i) { if(vis2[i]) continue; vector<int> path; int pos = i; while(!vis2[pos]) { path.push_back(pos); vis2[pos] = true; pos = S[pos]; } for(int j = int(path.size()) - 1; j >= 1; --j) { P[cl] = path[j - 1]; Q[cl] = path[j]; ++cl; } } vector<int> perm(N); for(int i = 0; i < N; ++i) { perm[i] = i; } for(int i = L - 1; i >= 0; --i) { P[i] = find(perm.begin(), perm.end(), P[i]) - perm.begin(); Q[i] = find(perm.begin(), perm.end(), Q[i]) - perm.begin(); swap(perm[X[i]], perm[Y[i]]); } return L; }

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:60:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   60 |   P[i] = find(perm.begin(), perm.end(), P[i]) - perm.begin();
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
sorting.cpp:61:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   61 |   Q[i] = find(perm.begin(), perm.end(), Q[i]) - perm.begin();
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#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...