Submission #405884

#TimeUsernameProblemLanguageResultExecution timeMemory
405884jk410Sorting (IOI15_sorting)C++17
20 / 100
2 ms332 KiB
#include <bits/stdc++.h> #include "sorting.h" using namespace std; int A[200000],tmp[200000],tmp_P[200000],tmp_Q[200000]; bool Visited[200000]; bool f(int N,int S[],int X[],int Y[],int k){ for (int i=0; i<N; i++){ tmp[i]=S[i]; Visited[i]=false; } for (int i=0; i<k; i++){ swap(tmp[X[i]],tmp[Y[i]]); tmp_P[i]=tmp_Q[i]=0; } int cnt=0; for (int i=0; i<N; i++){ if (tmp[i]!=i&&!Visited[i]){ Visited[i]=true; for (int j=tmp[i]; !Visited[j]; j=tmp[j]){ Visited[j]=true; tmp_P[cnt]=j; tmp_Q[cnt]=tmp[j]; cnt++; if (cnt>k) return false; } } } return true; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { int ans=0,l=0,r=N; for (int i=0; i<N; i++) A[S[i]]=i; while (l<=r){ int m=(l+r)>>1; if (f(N,S,X,Y,m)){ ans=m; r=m-1; } else l=m+1; } for (int i=0; i<ans; i++){ swap(A[S[X[i]]],A[S[Y[i]]]); swap(S[X[i]],S[Y[i]]); P[i]=A[tmp_P[i]]; Q[i]=A[tmp_Q[i]]; swap(S[A[tmp_P[i]]],S[A[tmp_Q[i]]]); swap(A[tmp_P[i]],A[tmp_Q[i]]); } return ans; }

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:31:39: warning: unused parameter 'M' [-Wunused-parameter]
   31 | 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...