#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "sorting.h"
const int N=1e6+99;
int n,m,a[N],b[N],s[N],t[N],pos[N],mark[N];
vector<pair<int,int>> ans;
int Cnt(){
fill(mark,mark+N,0);
int cnt=0;
f(i,0,n){
if(mark[i]) continue ;
cnt++;
for(int u=i;mark[u]==0;u=b[u]){
mark[u]=1;
}
}
return cnt;
}
bool check(int x){
f(i,0,n) b[i]=a[i];
f(i,0,x+1) swap(b[s[i]],b[t[i]]);
return n-Cnt()<=x+1;
}
void do_it(int x){
check(x);
fill(mark,mark+N,0);
f(i,0,n){
if(mark[i]) continue ;
mark[i]=1;
for(int u=b[i];mark[u]==0;u=b[u]){
ans.pb({i,u});
}
}
}
int findSwapPairs(int _n, int A[], int _m, int S[], int T[], int P[], int Q[]) {
n=_n,m=_m;
int c=1;
f(i,0,n){
a[i]=A[i];
if(a[i]!=i) c=0;
}
if(c) return 0;
f(i,0,m) s[i]=S[i],t[i]=T[i];
int l=-1,r=m;
while(l+1<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid;
}
r=m-1;
do_it(r);
if(ans.size()<r+1) ans.pb({0,0});
f(i,0,n) pos[a[i]]=i;
f(i,0,r+1){
swap(pos[a[s[i]]],pos[a[t[i]]]);
swap(a[s[i]],a[t[i]]);
P[i]=pos[ans[i].F];
Q[i]=pos[ans[i].S];
swap(pos[a[P[i]]],pos[a[Q[i]]]);
swap(a[P[i]],a[Q[i]]);
}
//dbga(a,0,n);
return r+1;
}
/*
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n,m,a[100],s[100],t[100],p[100],q[100];
cin>>n;
f(i,0,n) cin>>a[i];
cin>>m;
f(i,0,m) cin>>s[i]>>t[i];
cout<<findSwapPairs(n,a,m,s,t,p,q)<<"R"<<endl;
}*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:74:15: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
74 | if(ans.size()<r+1) ans.pb({0,0});
| ~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |