이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* Author: Nicholas Winschel
* Created: 05.09.2023 19:37:58
**/
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using db=long double;
template<class T> using V=vector<T>;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int,int>;
#define f first
#define s second
#define sz(x) (int)((x).size())
#include "sorting.h"
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
auto tr = [&](int l) -> bool {
// cout << l << endl;
vi s(N);
for (int i = 0; i < N; i++) {
s[i]=S[i];
}
for (int i = 0; i < l; i++) {
swap(s[X[i]],s[Y[i]]);
}
// for (int i = 0; i < N; i++) {
// cout << s[i] << " ";
// }
// cout << "\n";
V<bool> vis(N);
int cnt = 0;
auto dfs = [&](int v, auto &&dfs) -> void {
if (vis[v]) return;
vis[v]=true;
dfs(s[v], dfs);
};
for (int i = 0; i < N; i++) {
if (!vis[i]) {
cnt++;
dfs(i,dfs);
}
}
cnt = N-cnt;
return cnt <= l;
};
int l = -1, r = M;
while (r-l > 1) {
int m = (l+r)/2;
if (tr(m)) r=m;
else l=m;
}
vi s(N);
for (int i = 0; i < N; i++) {
s[i]=S[i];
}
for (int i = 0; i < r; i++) {
swap(s[X[i]],s[Y[i]]);
}
V<pi> pans;
V<bool> vis(N);
vi stk;
auto dfs = [&](int v, auto &&dfs) -> void {
if (vis[v]) return;
vis[v]=true;
dfs(s[v], dfs);
stk.push_back(v);
};
for (int i = 0; i < N; i++) {
if (!vis[i]) {
stk = vi();
dfs(i,dfs);
for (int j = 0; j < sz(stk)-1; j++) {
pans.emplace_back(stk[j], stk[j+1]);
// cout << stk[j] << " " << stk[j+1] << endl;
}
}
}
while (sz(pans) < r) pans.emplace_back(0,0);
vi cur(N); iota(cur.begin(), cur.end(), 0);
vi rcur=cur;
for (int i = r-1; i >= 0; i--) {
pi h = pans[i];
P[i] = rcur[h.f];
Q[i] = rcur[h.s];
swap(cur[X[i]], cur[Y[i]]);
rcur[cur[X[i]]]=X[i];
rcur[cur[Y[i]]]=Y[i];
}
return r;
}
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In lambda function:
sorting.cpp:36:38: warning: declaration of 'dfs' shadows a previous local [-Wshadow]
36 | auto dfs = [&](int v, auto &&dfs) -> void {
| ~~~~~~~^~~
sorting.cpp:36:14: note: shadowed declaration is here
36 | auto dfs = [&](int v, auto &&dfs) -> void {
| ^~~
sorting.cpp: In lambda function:
sorting.cpp:66:34: warning: declaration of 'dfs' shadows a previous local [-Wshadow]
66 | auto dfs = [&](int v, auto &&dfs) -> void {
| ~~~~~~~^~~
sorting.cpp:66:10: note: shadowed declaration is here
66 | auto dfs = [&](int v, auto &&dfs) -> void {
| ^~~
# | 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... |