#include <bits/stdc++.h>
using namespace std;
int lol[100001],vis[100001];
vector<int> v;
void dfs(int i){
vis[i] = 1;
v.push_back(i);
if(vis[lol[i]]==0)dfs(lol[i]);
}
//int P[100001] , Q[100001];
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
if(N==1)return 0;
bool ss =1;
if(X[0]==Y[0]){
ss = 0;
}
int ans = 0;
if(ss){
swap(S[0],S[1]);
int on = 0 , ze = 0;
for(int i = 0;i<N;i++){
if(S[i]==0)ze = i;
if(S[i]==1)on = i;
}
if(ze>1){
swap(S[(on?0:1)],S[ze]);
P[0] = ze , Q[0] = (on?0:1);
//cout<<ze<<"hh"<<endl;
ans++;
swap(S[0],S[1]);
if(on>1){
swap(S[0],S[on]);
P[1] = 0 , Q[1] = on;
ans++;
swap(S[0],S[1]);
}
}else if(on>1){
if(ze==0){
swap(S[1],S[on]);
P[0] = on;
Q[0] = 1;
ans++;
}else{
swap(S[0],S[on]);
P[0] = on;
Q[0] = 0;
ans++;
}
swap(S[0],S[1]);
}
}
for(int i = 0;i<N;i++){
lol[i] = S[i];
vis[i] = 0;
}
int ha = -1;
for(int i = (ss?2:0);i<N;i++){
if(vis[i]==0){
v.clear();
dfs(i);
for(int j = 1;j<v.size();j++){
ha++;
P[ans] = v[0];
Q[ans] = v[j];
ans++;
}
}
}
if(!ss){
return ans;
}
if(ha==-1){
if(S[0]==1){
return ans;
}else{
P[ans] = 0;
Q[ans] = 0;
ans++;
return ans;
}
}else{
int pr = ha%2;
if(S[pr]==0){
return ans;
}else{
P[ans] = 0;
Q[ans] = 0;
ans++;
return ans;
}
}
}
/*
int main(){
int S[] = {0,4,1,3,2};
0 4 1 3 2
4 0 1 3 2 E
1 0 4 3 2 A
0 1 4 3 2
int X[] = {0,0,0,0,0,0,0,0,0,0,0};
int Y[] = {1,1,1,1,1,1,1,1,1,1,1};
cout<<findSwapPairs(5,S,100,X,Y)<<endl;
for(int i = 0;i<10;i++){
cout<<P[i]<<" "<<Q[i]<<endl;
}
}
*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:61:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int j = 1;j<v.size();j++){
| ~^~~~~~~~~
sorting.cpp:11:37: warning: unused parameter 'M' [-Wunused-parameter]
11 | int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
| ~~~~^
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |