이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int n, int arr[], int m, int x_moves[], int y_moves[], int x_ans[], int y_ans[]){
vector<int> fin_arr(n), x_todo, y_todo;
auto calc = [&](int mid)->int{
x_todo.clear(); y_todo.clear();
for(int i = 0; i<n; ++i) fin_arr[i] = arr[i];
for(int i = 0; i<mid; ++i)
swap(fin_arr[x_moves[i]], fin_arr[y_moves[i]]);
for(int i = 0; i<n; ++i)
while(fin_arr[i]!=i)
x_todo.push_back(fin_arr[i]), y_todo.push_back(fin_arr[fin_arr[i]]),
swap(fin_arr[i], fin_arr[fin_arr[i]]);
return (int)x_todo.size();
};
int lo = 0, hi = m;
while(lo<hi){
int mid = lo+(hi-lo)/2;
if(calc(mid)<=mid) hi = mid;
else lo = mid+1;
} calc(lo);
while(x_todo.size()<lo)
x_todo.push_back(0), y_todo.push_back(0);
vector<int> pos(n);
for(int i = 0; i<n; ++i)
pos[arr[i]] = i;
for(int i = 0; i<lo; ++i)
swap(pos[arr[x_moves[i]]], pos[arr[y_moves[i]]]), swap(arr[x_moves[i]], arr[y_moves[i]]),
x_ans[i] = pos[x_todo[i]], y_ans[i] = pos[y_todo[i]],
swap(pos[arr[x_ans[i]]], pos[arr[y_ans[i]]]), swap(arr[x_ans[i]], arr[y_ans[i]]);
return lo;
}
컴파일 시 표준 에러 (stderr) 메시지
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:23:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
23 | while(x_todo.size()<lo)
| ~~~~~~~~~~~~~^~~
# | 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... |