This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5+5;
int n, m;
int x[N], y[N], pos[N], a[N], orig[N];
vector<pair<int, int>> v, v1;
bool check(int mid){
v.clear();v1.clear();
for(int i=0;i<mid;i++){
swap(a[x[i]], a[y[i]]);
}for(int i=0;i<n;i++)pos[a[i]] = i;
int ans = 0;
for(int i=0;i<n;i++){
if(pos[i] == i){
continue;
}else {
ans++;
int f = i, s = pos[i];
v1.push_back({a[f], a[s]});
swap(a[f], a[s]);
swap(pos[a[f]], pos[a[s]]);
}
}
if(ans > mid)return 0;
int j=0;
for(int i=0;i<n;i++)a[i] = orig[i], pos[orig[i]] = i;
for(int i=0;i<mid;i++){
if(j >= v1.size())v.push_back({0,0});
else {
swap(a[x[i]], a[y[i]]);
swap(pos[a[x[i]]], pos[a[y[i]]]);
int f = v1[j].first, s = v1[j].second;
v.push_back({pos[f], pos[s]});
swap(a[pos[f]], a[pos[s]]);
swap(pos[a[pos[f]]], pos[a[pos[s]]]);
}j++;
}return 1;
}
int findSwapPairs(int NN, int A[], int MM, int X[], int Y[], int P[], int Q[]) {
n = NN, m = MM;
int l = -1, r = m;
for(int i=0;i<m;i++){
x[i] = X[i];
y[i] = Y[i];
}
while(l+1 < r){
for(int i=0;i<n;i++)a[i] = A[i], orig[i] = A[i];
int mid = (l+r)/2;
if(check(mid)){
r = mid;
}else {
l = mid;
}
}for(int i=0;i<n;i++)a[i] = A[i];
check(r);
for(int i=0;i<r;i++){
if(i < v.size()){
P[i] = v[i].first;
Q[i] = v[i].second;
}else {
P[i] = Q[i] = 0;
}
}
return r;
}
Compilation message (stderr)
sorting.cpp: In function 'bool check(int)':
sorting.cpp:29:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(j >= v1.size())v.push_back({0,0});
| ~~^~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:58:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(i < v.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... |