Submission #291569

#TimeUsernameProblemLanguageResultExecution timeMemory
291569MarcoMeijerSorting (IOI15_sorting)C++14
74 / 100
1098 ms8696 KiB
#include <bits/stdc++.h> using namespace std; #include "sorting.h" //macros typedef long long ll; typedef pair<int, int> ii; typedef pair<ll, ll> lll; typedef tuple<int, int, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<ll> vll; typedef vector<lll> vlll; #define REP(a,b,c) for(int a=int(b); a<int(c); a++) #define RE(a,c) REP(a,0,c) #define RE1(a,c) REP(a,1,c+1) #define REI(a,b,c) REP(a,b,c+1) #define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--) #define INF 1e9 #define pb push_back #define fi first #define se second #define sz size() const int MX = 2e5+100; int a[MX], sa[MX], ss[MX]; int b[MX]; int n; int *S, *X, *Y; void swapA(int x, int y) { swap(a[x], a[y]); sa[a[x]] = x; sa[a[y]] = y; } void swapS(int x, int y) { swap(S[x], S[y]); ss[S[x]] = x; ss[S[y]] = y; } int visited[MX]; void dfs(int u) { if(visited[u]) return; visited[u] = 1; dfs(b[u]); } bool possible(int swaps) { RE(i,n) visited[i] = 0; int cyc = 0; RE(i,n) { if(visited[i]) continue; dfs(i); cyc++; } if(n-cyc <= swaps) return 1; return 0; } int findSwapPairs(int _n, int _S[], int m, int _X[], int _Y[], int P[], int Q[]) { n=_n, S=_S, X=_X, Y=_Y; RE(i,n) a[i]=sa[i]=i; RE(i,n) b[i] = S[i]; RE(i,n) ss[S[i]] = i; int times = 0; RE(i,n) { if(possible(i)) break; swapA(sa[X[i]], sa[Y[i]]); swap(b[X[i]], b[Y[i]]); times++; } int R = 0; int curI = 0; RE(i,times) { R++; swapA(X[i], Y[i]); swapS(X[i], Y[i]); int x=0, y=0; while(x == y) { if(curI == n) break; x = ss[curI]; y = sa[curI]; curI++; } swapS(x, y); P[R-1] = x; Q[R-1] = y; } return R; }

Compilation message (stderr)

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