#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=2020;
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(){
fill(mark,mark+N,0);
ans.clear();
f(i,0,n){
if(mark[i]) continue ;
vector<int> vec;
for(int u=i;mark[u]==0;u=b[u]){
mark[u]=1;
vec.pb(u);
}
f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
}
}
int findSwapPairs(int _n, int A[], int _m, int S[], int T[], int P[], int Q[]) {
n=_n,m=_m;
f(i,0,n) a[i]=A[i];
f(i,0,m) P[i]=Q[i]=0;
/*int cnt=0;
f(i,0,n){
while(A[i]!=i){
P[cnt]=i;
Q[cnt]=A[i];
swap(A[P[cnt]],A[Q[cnt]]);
cnt++;
}
}
return cnt;
int c=1;
f(i,0,n){
a[i]=A[i];
s[i]=S[i];
t[i]=T[i];
if(a[i]!=i) c=0;
}
if(c) return 0;
int l=-1,r=m;
while(l+1<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid;
}*/
int r=m-1;
int c=check(r);
do_it();
while(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]]);
}
f(i,0,n) if(a[i]!=i){
assert(0);
while(1){
swap(a[0],a[1]);
}
}
return r+1;
}
/*
int32_t main(){
srand(time(NULL));
int n,m,a[100],s[100],t[100],p[100],q[100];
cin>>n>>m;
//f(i,0,n) cin>>a[i];
iota(a,a+n,0); //random_shuffle(a,a+n);
do{
f(i,0,m) s[i]=t[i]=0;
dbga(a,0,n);
cout<<findSwapPairs(n,a,m,s,t,p,q)<<"R"<<'\n';
} while(next_permutation(a,a+n));
}*/
Compilation message
sorting.cpp: In function 'void do_it()':
sorting.cpp:56:5: warning: declaration of 'i' shadows a previous local [-Wshadow]
56 | f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
| ^
sorting.cpp:8:26: note: in definition of macro 'f'
8 | #define f(i,a,b) for(int i=a;i<b;i++)
| ^
sorting.cpp:49:4: note: shadowed declaration is here
49 | f(i,0,n){
| ^
sorting.cpp:8:26: note: in definition of macro 'f'
8 | #define f(i,a,b) for(int i=a;i<b;i++)
| ^
sorting.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
56 | f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
| ~~~~~~~~~~~~~~
sorting.cpp:56:3: note: in expansion of macro 'f'
56 | f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
| ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:91:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
91 | while(ans.size()<r+1) ans.pb({0,0});
| ~~~~~~~~~~^~~~
sorting.cpp:89:6: warning: unused variable 'c' [-Wunused-variable]
89 | int c=check(r);
| ^
sorting.cpp:20:11: warning: unused parameter 'second' [-Wunused-parameter]
20 | #define S second
| ^
sorting.cpp:59:48: note: in expansion of macro 'S'
59 | int findSwapPairs(int _n, int A[], int _m, int S[], int T[], int P[], int Q[]) {
| ^
sorting.cpp:59:57: warning: unused parameter 'T' [-Wunused-parameter]
59 | int findSwapPairs(int _n, int A[], int _m, int S[], int T[], 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 |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
852 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
852 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |