이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int countCycles(vector<int> p) {
vector<bool> vis(p.size());
int count=0;
for (int i = 0; i<(int)p.size(); i++) {
if (!vis[i]) count++;
int pos = i;
while(!vis[pos]) {
vis[pos] = 1;
pos = p[pos];
}
}
return count;
}
vector<int> doSwaps(vector<int> input, vector<pair<int, int>> swaps, int number) {
for (int i = 0; i<number; i++) {
swap(input[swaps[i].first], input[swaps[i].second]);
}
return input;
}
int findSwapPairs(int N, int _S[], int M, int X[], int Y[], int P[], int Q[]) {
vector<int> S(N);
for (int i = 0; i<N; i++) S[i] = _S[i];
vector<pair<int, int>> swapsOther;
for (int i = 0; i<M; i++) swapsOther.push_back({X[i], Y[i]});
int l = 0, r = M;
while (l < r) {
int m = (l+r)/2;
int cycles = countCycles(doSwaps(S, swapsOther, m));
// cerr << m << " " << cycles << "\n";
if (N-cycles <= m) {
r = m;
} else {
l = m+1;
}
}
vector<int> hisPerm = doSwaps(S, swapsOther, l);
// cerr << l << "\n";
// for (int i: hisPerm) cerr << i << " ";
// cerr << "\n";
vector<pair<int, int>> mySwapsValues;
vector<int> hisPermInv(N);
for (int i = 0; i<N; i++) {
hisPermInv[hisPerm[i]] = i;
}
for (int i = 0; i<N; i++) {
if (hisPerm[i] == i) continue;
mySwapsValues.push_back({i, hisPerm[i]});
int miau = hisPerm[i];
swap(hisPerm[hisPermInv[i]], hisPerm[i]);
swap(hisPermInv[i], hisPermInv[miau]);
}
// for (int i: hisPerm) cerr << i << " ";
// cerr << "\n";
assert(is_sorted(hisPerm.begin(), hisPerm.end()));
// for (auto i: mySwapsValues) {cerr << i.first << " " << i.second << "\n";}
vector<int> SInv(N);
for (int i = 0; i<N; i++) SInv[S[i]] = i;
for (int i = 0; i<mySwapsValues.size(); i++) {
swap(SInv[S[swapsOther[i].first]], SInv[S[swapsOther[i].second]]);
swap(S[swapsOther[i].first], S[swapsOther[i].second]);
P[i] = SInv[mySwapsValues[i].first];
Q[i] = SInv[mySwapsValues[i].second];
assert(S[P[i]] == mySwapsValues[i].first);
assert(S[Q[i]] == mySwapsValues[i].second);
swap(SInv[S[P[i]]], SInv[S[Q[i]]]);
swap(S[P[i]], S[Q[i]]);
}
return mySwapsValues.size();
}
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i<mySwapsValues.size(); i++) {
| ~^~~~~~~~~~~~~~~~~~~~~
sorting.cpp:102:30: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
102 | return mySwapsValues.size();
| ~~~~~~~~~~~~~~~~~~^~
# | 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... |