#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
int l=0,r=M;
int n=N;
while(r>l){
int m=(l+r)/2;
vector<int> v(n);
iota(v.begin(),v.end(),0);
for(int i=0;i<m;i++){
swap(v[X[i]],v[Y[i]]);
}
vector<int> g(n);
for(int i=0;i<n;i++){
g[v[i]]=S[i];
}
vector<int> vis(n);
int cnt=0;
for(int i=0;i<n;i++){
if(!vis[i]){
cnt++;
vis[i]=1;
int a=i;
while(1){
a=g[a];
if(vis[a]){
break;
}
vis[a]=1;
}
}
}
int t=n-cnt;
if(t<=m){
r=m;
}
else{
l=m+1;
}
}
vector<int> v(n);
iota(v.begin(),v.end(),0);
for(int i=0;i<l;i++){
swap(v[X[i]],v[Y[i]]);
}
vector<int> d(n),e(n);
for(int i=0;i<n;i++){
d[S[i]]=i;
e[v[i]]=i;
//g[v[i]]={S[i],i};
}
vector<int> vis(n);
int cnt=0;
int c=0,cn=0;
for(int i=0;i<l;i++){
{
int t=d[X[i]],f=e[Y[i]];
swap(d[S[t]],d[S[f]]);
swap(S[t],S[f]);
swap(e[v[t]],e[v[f]]);
swap(v[t],v[f]);
}
while(cn<n){
if(d[cn]!=e[cn]){
int t=d[cn],f=e[cn];
swap(d[S[t]],d[S[f]]);
swap(S[t],S[f]);
P[c]=t;
Q[c]=f;
assert(d[cn]==e[cn]);
c++;
cn++;
break;
}
cn++;
}
}
assert(c==l);
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:59:9: warning: unused variable 'cnt' [-Wunused-variable]
59 | int cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |