이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
for (int i = 0; i < M; i++) P[i] = Q[i] = 0;
int T[N];
for (int i = 0; i < N; i++) T[i] = S[i];
for (int i = 0; i < M; i++) swap(T[X[i]], T[Y[i]]);
int where[N];
for (int i = 0; i < N; i++) where[T[i]] = i;
vector<pair<int, int>> swap_list;
for (int i = 0; i < N; i++) {
if (T[i] != i) {
swap_list.emplace_back(i, T[i]);
int a = i, b = where[i];
swap(where[T[a]], where[T[b]]);
swap(T[a], T[b]);
}
}
for (int i = 0; i < N; i++) T[i] = S[i];
for (int i = 0; i < N; i++) where[T[i]] = i;
for (int i = 0; i < (int)swap_list.size(); i++) {
swap(T[X[i]], T[Y[i]]);
int a = where[swap_list[i].first], b = where[swap_list[i].second];
P[i] = a;
Q[i] = b;
swap(where[T[a]], where[T[b]]);
swap(T[a], T[b]);
}
for (int i = 0; i < M; i++) {
if (is_sorted(S, S+N)) return i;
swap(S[X[i]], S[Y[i]]);
swap(S[P[i]], S[Q[i]]);
}
assert(is_sorted(S, S+N));
return M;
}
// int main(int argc, char *argv[]) {
// if (argc > 1) srand(atoi(argv[1]));
// int N = rand() % 50 + 1;
// int M = 30*N;
// int S[N], X[M], Y[M], P[M], Q[M];
// iota(S, S+N, 0);
// random_shuffle(S, S+N);
// for (int i = 0; i < N; i++) {
// cerr << S[i] << " \n"[i == N-1];
// }
// for (int i = 0; i < M; i++) {
// int a = rand() % N, b = rand() % N;
// if (a > b) swap(a, b);
// X[i] = 0, Y[i] = 0;
// }
// findSwapPairs(N, S, M, X, Y, P, Q);
// return 0;
// }
# | 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... |