#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 n;
void print(vector<int> s){
for(int i=0;i<n;i++){
cout << s[i] << " ";
}
cout << "\n";
}
void print2(int s[]){
for(int i=0;i<n;i++){
cout << s[i] << " ";
}
cout << "\n";
}
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
int l=0,r=M;
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]]=i;
}
v=g;
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> g(n);
for(int i=0;i<n;i++){
g[v[i]]=i;
}
v=g;
vector<int> d(n),e(n);
for(int i=0;i<n;i++){
d[S[i]]=i;
e[v[i]]=i;
}
vector<int> vis(n);
int c=0,cn=0;
set<int> u;
for(int i=0;i<n;i++){
if(v[i]!=S[i]){
u.insert(i);
}
}
for(int i=0;i<l;i++){
{
int t=X[i],f=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]);
if(v[t]!=S[t]){
u.insert(t);
}
else{
u.erase(t);
}
if(v[f]!=S[f]){
u.insert(f);
}
else{
u.erase(f);
}
}
if(!u.size()){
continue;
}
auto cn=*u.begin();
u.erase(cn);
assert(v[cn]!=S[cn]);
if(v[cn]!=S[cn]){
int t=cn,f=d[v[cn]];
swap(d[S[t]],d[S[f]]);
swap(S[t],S[f]);
if(v[t]!=S[t]){
u.insert(t);
}
else{
u.erase(t);
}
if(v[f]!=S[f]){
u.insert(f);
}
else{
u.erase(f);
}
P[c]=t;
Q[c]=f;
c++;
}
}
if(c<l){
P[c]=Q[c]=0;
c++;
}
return l;
}
/*
int main(){
int N, M;
cin >> N;
int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
for (int i = 0; i < N; ++i)
cin >> S[i];
cin >> M;
int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
for (int i = 0; i < M; ++i) {
cin >> X[i];
cin >> Y[i];
}
int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
int ans = findSwapPairs(N, S, M, X, Y, P, Q);
cout << ans << "\n";
for (int i = 0; i < ans; ++i)
cout << P[i] << " " << Q[i] << "\n";
return 0;
}
*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:110:14: warning: declaration of 'cn' shadows a previous local [-Wshadow]
110 | auto cn=*u.begin();
| ^~
sorting.cpp:80:13: note: shadowed declaration is here
80 | int c=0,cn=0;
| ^~
sorting.cpp:80:13: warning: unused variable 'cn' [-Wunused-variable]
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
440 KB |
Output is correct |
23 |
Correct |
1 ms |
568 KB |
Output is correct |
24 |
Correct |
2 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
568 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
568 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
279 ms |
21892 KB |
Output is correct |
16 |
Correct |
308 ms |
22280 KB |
Output is correct |
17 |
Correct |
381 ms |
23344 KB |
Output is correct |
18 |
Correct |
88 ms |
10204 KB |
Output is correct |
19 |
Correct |
249 ms |
16920 KB |
Output is correct |
20 |
Correct |
281 ms |
19112 KB |
Output is correct |
21 |
Correct |
284 ms |
19372 KB |
Output is correct |
22 |
Correct |
279 ms |
20804 KB |
Output is correct |
23 |
Correct |
315 ms |
24236 KB |
Output is correct |
24 |
Correct |
395 ms |
23896 KB |
Output is correct |
25 |
Correct |
399 ms |
23652 KB |
Output is correct |
26 |
Correct |
245 ms |
18624 KB |
Output is correct |
27 |
Correct |
213 ms |
16968 KB |
Output is correct |
28 |
Correct |
363 ms |
23772 KB |
Output is correct |
29 |
Correct |
365 ms |
23096 KB |
Output is correct |
30 |
Correct |
177 ms |
16716 KB |
Output is correct |
31 |
Correct |
371 ms |
23876 KB |
Output is correct |
32 |
Correct |
303 ms |
21716 KB |
Output is correct |
33 |
Correct |
401 ms |
23832 KB |
Output is correct |
34 |
Correct |
334 ms |
23896 KB |
Output is correct |
35 |
Correct |
273 ms |
18468 KB |
Output is correct |
36 |
Correct |
85 ms |
11160 KB |
Output is correct |
37 |
Correct |
496 ms |
24864 KB |
Output is correct |
38 |
Correct |
475 ms |
23904 KB |
Output is correct |
39 |
Correct |
434 ms |
24224 KB |
Output is correct |