이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 2e5 + 10;
int n, m;
vector<pair<int, int> > swps;
vector<int> start;
bool used[MAX_N];
int inv[MAX_N];
int countCycles(vector<int> &a) {
int cnt = 0;
for(int i = 0; i < a.size(); i ++) {used[i] = false;}
for(int i = 0; i < a.size(); i ++) {
if(used[i]) {continue;}
cnt ++;
int curr = i;
while(!used[curr]) {
used[curr] = true;
curr = a[curr];
}
}
return cnt;
}
vector<pair<int, int> > getPairs(vector<int> &a) {
vector<pair<int, int> > ret;
for(int i = 0; i < a.size(); i ++) {used[i] = false;}
for(int i = 0; i < a.size(); i ++) {
if(used[i]) {continue;}
int curr = i, ind = i;
while(!used[curr]) {
used[curr] = true;
curr = a[curr];
if(curr != i) {
ret.push_back({curr, ind});
}
ind = curr;
}
}
return ret;
}
bool eval(int x) {
vector<int> toAsk = start;
for(int i = 0; i < x; i ++) {
swap(toAsk[swps[i].first], toAsk[swps[i].second]);
}
//cout << "Eval " << x << " " << countCycles(toAsk) << endl;
return n - countCycles(toAsk) <= x;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
m = M;
for(int i = 0; i < n; i ++) {
start.push_back(S[i]);
}
for(int i = 0; i < m; i ++) {
swps.push_back({X[i], Y[i]});
}
if(countCycles(start) == n) {
//cout << "Here " << endl;
return 0;
}
int l = 0, r = m;
while(l < r - 1) {
int mid = (l + r) / 2ll;
if(eval(mid)) {
r = mid;
} else {
l = mid;
}
}
vector<int> atEnd = start;
for(int i = 0; i < r; i ++) {
swap(atEnd[swps[i].first], atEnd[swps[i].second]);
}
for(int i = 0; i < n; i ++) {
inv[atEnd[i]] = i;
//cout << atEnd[i] << " ";
}
//cout << endl;
auto toRet = getPairs(atEnd);
vector<pair<int, int> > ret;
for(int i = 0; i < r; i ++) {
int ind = r - 1 - i;
if(i >= toRet.size()) {
ret.push_back({0, 0});
} else {
//cout << "Need " << toRet[i].first << " " << toRet[i].second << endl;
ret.push_back({inv[toRet[i].first], inv[toRet[i].second]});
}
swap(inv[atEnd[swps[i].first]], inv[atEnd[swps[i].second]]);
swap(atEnd[swps[i].first], atEnd[swps[i].second]);
}
reverse(ret.begin(), ret.end());
for(int i = 0; i < r; i ++) {
P[i] = ret[i].first;
Q[i] = ret[i].second;
}
return r;
}
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In function 'int countCycles(std::vector<int>&)':
sorting.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0; i < a.size(); i ++) {used[i] = false;}
| ~~^~~~~~~~~~
sorting.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < a.size(); i ++) {
| ~~^~~~~~~~~~
sorting.cpp: In function 'std::vector<std::pair<int, int> > getPairs(std::vector<int>&)':
sorting.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < a.size(); i ++) {used[i] = false;}
| ~~^~~~~~~~~~
sorting.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < a.size(); i ++) {
| ~~^~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:98:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | if(i >= toRet.size()) {
| ~~^~~~~~~~~~~~~~~
sorting.cpp:97:13: warning: unused variable 'ind' [-Wunused-variable]
97 | int ind = r - 1 - i;
| ^~~
# | 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... |