#include<bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int nmax=2e5+42;
int arr[nmax],where[nmax],inv[nmax];
bool been[nmax];
vector< pair<int,int> > change;
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[])
{
memset(been,0,sizeof(been));
change={};
int ok=N,not_ok=-1;
while(ok-not_ok>1)
{
int av=(ok+not_ok)/2;
//test av
for(int j=0;j<N;j++)
arr[j]=S[j];
for(int j=0;j<av;j++)
swap(arr[X[j]],arr[Y[j]]);
memset(been,0,sizeof(been));
int now=0;
for(int i=0;i<N;i++)
if(been[i]==0)
{
int j=i,d=0;
while(been[j]==0)
{
been[j]=1;
d++;
j=arr[j];
}
now=now+d-1;
}
//cout<<av<<" -> "<<now<<endl;
if(now<=av)ok=av;
else not_ok=av;
}
for(int j=0;j<N;j++)
arr[j]=S[j];
for(int j=0;j<ok;j++)
swap(arr[X[j]],arr[Y[j]]);
memset(been,0,sizeof(been));
for(int i=0;i<N;i++)
if(been[i]==0)
{
int j=i,d=0;
while(been[j]==0)
{
been[j]=1;
d++;
j=arr[j];
if(i!=j)change.push_back({min(i,j),max(i,j)});
}
}
while(change.size()<ok)
{
change.push_back({0,0});
}
for(auto k:change)
{
swap(arr[k.first],arr[k.second]);
}
for(int i=0;i<N;i++)
if(arr[i]!=i)return -1;//???
int SZ=change.size();
//reverse(change.begin(),change.end());
/*
cout<<"change: "<<endl;
for(auto k:change)cout<<k.first<<" "<<k.second<<endl;
*/
/*
for(int i=0;i<SZ;i++)
{
pair<int,int> now=change[i];
for(int j=ok-1;j>i;j--)
{
if(X[j]==now.first&&Y[j]==now.second)continue;
if(X[j]==now.second&&Y[j]==now.first)continue;
int a=-1,b=-1,q=-1;
if(X[j]==now.first)a=X[j],b=Y[j],q=now.second;
else if(Y[j]==now.first)a=Y[j],b=X[j],q=now.second;
else if(X[j]==now.second)a=X[j],b=Y[j],q=now.first;
else if(Y[j]==now.second)a=Y[j],b=X[j],q=now.first;
if(a!=-1)now={b,q};
//cout<<"after "<<j<<" step, "<<now.first<<" "<<now.second<<endl;
}
P[i]=now.first;
Q[i]=now.second;
//cout<<i<<" -> "<<now.first<<" "<<now.second<<endl;
}
*/
for(int j=0;j<N;j++)
where[j]=j,inv[j]=j;
for(int i=ok-1;i>=0;i--)
{
P[i]=where[change[i].first];
Q[i]=where[change[i].second];
swap(where[inv[X[i]]],where[inv[Y[i]]]);
swap(inv[X[i]],inv[Y[i]]);
}
//test
for(int j=0;j<N;j++)
arr[j]=S[j];
for(int i=0;i<ok;i++)
{
swap(arr[X[i]],arr[Y[i]]);
swap(arr[P[i]],arr[Q[i]]);
//for(int j=0;j<N;j++)cout<<arr[j]<<" ";cout<<endl;
}
for(int i=0;i<N;i++)
if(arr[i]!=i)
{
cout<<"---"<<endl;
cout<<ok<<endl;
for(int j=0;j<ok;j++)cout<<P[j]<<" "<<Q[j]<<endl;
for(int j=0;j<N;j++)cout<<arr[j]<<" ";cout<<endl;
system("pause");
}
return ok;
}
/*
int n=5;
int S[5]={4,3,2,1,0};
int m=6;
int X[6]={0,1,2,3,0,1};
int Y[6]={1,2,3,4,1,2};
int P[6];
int Q[6];
*/
/*
int n=3;
int S[3]={0,2,1};
int m=3;
int X[3]={0,1,2};
int Y[3]={1,2,0};
int P[3];
int Q[3];
*/
/*
const int n=5;
int S[n];
const int m=6;
int X[m]={1,4,2,1,0};
int Y[m]={1,0,3,4,4};
int P[m];
int Q[m];
int main()
{
srand(time(0));
while(1)
{
for(int j=0;j<n;j++)S[j]=j;
random_shuffle(S,S+n);
for(int j=0;j<m;j++)
{
X[j]=rand()%n;
Y[j]=rand()%n;
}
cout<<n<<" : ";for(int j=0;j<n;j++)cout<<S[j]<<" ";cout<<endl;
cout<<m<<endl;
for(int j=0;j<m;j++)cout<<X[j]<<" "<<Y[j]<<endl;
cout<<findSwapPairs(n,S,m,X,Y,P,Q)<<endl;
cout<<endl;
}
}
*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:60:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(change.size()<ok)
~~~~~~~~~~~~~^~~
sorting.cpp:71:23: warning: conversion to 'int' from 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' may alter its value [-Wconversion]
int SZ=change.size();
~~~~~~~~~~~^~
sorting.cpp:123:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(int j=0;j<N;j++)cout<<arr[j]<<" ";cout<<endl;
^~~
sorting.cpp:123:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for(int j=0;j<N;j++)cout<<arr[j]<<" ";cout<<endl;
^~~~
sorting.cpp:71:9: warning: unused variable 'SZ' [-Wunused-variable]
int SZ=change.size();
^~
sorting.cpp:8:37: warning: unused parameter 'M' [-Wunused-parameter]
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[])
^
sorting.cpp:124:19: warning: ignoring return value of 'int system(const char*)', declared with attribute warn_unused_result [-Wunused-result]
system("pause");
~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
612 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
612 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
4 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
4 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
152 ms |
11892 KB |
Output is correct |
16 |
Correct |
182 ms |
12136 KB |
Output is correct |
17 |
Correct |
219 ms |
13032 KB |
Output is correct |
18 |
Correct |
62 ms |
7800 KB |
Output is correct |
19 |
Correct |
157 ms |
10860 KB |
Output is correct |
20 |
Correct |
160 ms |
11372 KB |
Output is correct |
21 |
Correct |
168 ms |
11628 KB |
Output is correct |
22 |
Correct |
171 ms |
12884 KB |
Output is correct |
23 |
Correct |
176 ms |
13292 KB |
Output is correct |
24 |
Correct |
214 ms |
13288 KB |
Output is correct |
25 |
Correct |
209 ms |
13416 KB |
Output is correct |
26 |
Correct |
153 ms |
11244 KB |
Output is correct |
27 |
Correct |
138 ms |
10568 KB |
Output is correct |
28 |
Correct |
179 ms |
12904 KB |
Output is correct |
29 |
Correct |
193 ms |
12776 KB |
Output is correct |
30 |
Correct |
100 ms |
9840 KB |
Output is correct |
31 |
Correct |
206 ms |
13160 KB |
Output is correct |
32 |
Correct |
176 ms |
12648 KB |
Output is correct |
33 |
Correct |
216 ms |
13388 KB |
Output is correct |
34 |
Correct |
192 ms |
13164 KB |
Output is correct |
35 |
Correct |
139 ms |
10860 KB |
Output is correct |
36 |
Correct |
49 ms |
8312 KB |
Output is correct |
37 |
Correct |
222 ms |
14184 KB |
Output is correct |
38 |
Correct |
214 ms |
13456 KB |
Output is correct |
39 |
Correct |
247 ms |
13680 KB |
Output is correct |