이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[]) {
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;
cont = 0;
rep(i,0,n-1) {
if (visitados[i] == 0) {
act = doble[i];
pas = i;
visitados[i] = 1;
while (act != i) {
P[cont] = pas;
Q[cont] = act;
cont++;
visitados[act] = 1;
pas = act;
act = doble[act];
}
}
}
//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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |