#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 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;
}
c=check(r);
do_it();
while(ans.size()<r+1) ans.pb({0,0});
f(i,0,n) pos[a[i]]=i;
dbga(a,0,n);
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);
}
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:79: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]
79 | while(ans.size()<r+1) ans.pb({0,0});
| ~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Expected integer, but "a" found |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Expected integer, but "a" found |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Expected integer, but "a" found |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Expected integer, but "a" found |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |