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;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m;
vector<int> p;
vector<int> x,y;
vector<pair<int,int>> vec;
bool check(int mid){
vector<int> pp=p;
for(int i=0;i<mid;i++){
swap(pp[x[i]],pp[y[i]]);
}
vector<bool> vis(n);
vec.clear();
for(int i=0;i<n;i++){
if(vis[i]) continue;
int cur=i;
while(!vis[cur]){
vis[cur]=true;
cur=pp[cur];
if(cur!=i){
vec.push_back({i,cur});
}
}
}
if((int)vec.size()<=mid) return true;
else return false;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
m=M;
p.resize(n);
x.resize(m);
y.resize(m);
for(int i=0;i<n;i++){
p[i]=S[i];
}
for(int i=0;i<m;i++){
x[i]=X[i];
y[i]=Y[i];
}
int l=0,r=m;
while(l<r){
int mid=(l+r)/2;
if(check(mid)){
r=mid;
}
else{
l=mid+1;
}
}
check(l);
for(int i=0;i<(int)vec.size();i++){
P[i]=vec[i].f;
Q[i]=vec[i].s;
}
return l;
}
# | 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... |