Submission #286753

#TimeUsernameProblemLanguageResultExecution timeMemory
286753OzySorting (IOI15_sorting)C++17
100 / 100
191 ms26616 KiB
#include "sorting.h" #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i = (a); i <= (b); i++) #define debug(a) cout << #a << " = " << a << endl int sum,largo,act,cont; int seg,n,m,w,pas; int s[200001],x[600001],y[600001],doble[200001],visitados[200001],id[200001]; bool checa(int pos) { rep(i,0,n-1) visitados[i] = 0; rep(i,0,n-1) doble[i] = s[i]; rep(i,0,pos) swap(doble[x[i]],doble[y[i]]); sum = 0; rep(i,0,n-1) { if (visitados[i] == 0) { act = doble[i]; largo = 0; visitados[i] = 1; while (act != i) { largo++; visitados[act] = 1; act = doble[act]; } sum += largo; } } if (sum <= (pos+1)) return true; else return false; } void binaria(int ini, int fin) { int mitad; if (ini <= fin) { mitad = (ini+fin)/2; if (checa(mitad)) { seg = mitad; binaria(ini,mitad-1); } else binaria(mitad+1 ,fin); } } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { rep(i,0,N-1) if (S[i] == i) cont++; if (cont == N) return 0; n = N; m = M; rep(i,0,N-1) s[i] = S[i]; rep(i,0,M-1) x[i] = X[i]; rep(i,0,M-1) y[i] = Y[i]; binaria(0,M-1); rep(i,0,n-1) visitados[i] = 0; rep(i,0,n-1) doble[i] = s[i]; rep(i,0,seg) swap(doble[x[i]],doble[y[i]]); //rep(i,0,n-1) cout << s[i] << ' '; //cout << endl; //rep(i,0,n-1) cout << doble[i] << ' '; //cout << endl; rep(i,0,n-1) id[doble[i]] = i; cont = 0; rep(i,0,n-1) { if (visitados[i] == 0) { act = doble[i]; pas = doble[act]; visitados[i] = 1; while (visitados[id[pas]] == 0) { P[cont] = act; Q[cont] = pas; cont++; visitados[id[pas]] = 1; act = pas; pas = doble[pas]; } } } //debug(cont); //debug(seg); while (cont <= seg) { P[cont] = 0; Q[cont] = 0; cont++; } rep(i,0,n-1) id[s[i]] = i; rep(i,0,seg) { swap(id[ s[ x[i] ] ],id[ s[ y[i] ] ]); swap(s[x[i]], s[y[i]] ); swap(s[ id [ P[i]] ], s[ id [Q[i]] ]); swap(id[ P[i] ], id[ Q[i] ]); P[i] = id[P[i]]; Q[i] = id[Q[i]]; } //rep(i,0,seg) cout << P[i] << ' ' << Q[i] << endl; seg++; //cerr << seg; return seg; }
#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...