#include "sorting.h"
#include <bits/stdc++.h>
#define REP(v, i, j) for (int v = i; v != j; v++)
#define FORI(v) for (auto i : v)
#define FORJ(v) for (auto j : v)
#define OUT(v, a) \
FORI(v) \
cout << i << a;
#define OUTS(v, a, b) \
cout << v.size() << a; \
OUT(v, b)
#define in(a, n) \
REP(i, 0, n) \
cin >> a[i];
#define SORT(v) sort(begin(v), end(v))
#define REV(v) reverse(begin(v), end(v))
#define MEMSET(m) memset(m, -1, sizeof m)
#define pb push_back
#define fi first
#define se second
#define detachIO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;
const int MOD = 1e9+7;
struct modint {
long long val;
modint() = default;
modint(int _val): val(_val){}
modint operator+(modint b){ return ((this->val + b.val)%MOD); }
modint operator-(modint b){ return ((MOD + this->val - b.val)%MOD); }
modint operator*(modint b){ return ((this->val * b.val)%MOD); }
modint operator^(int a){
if(a==0)return 1;
if(a==1)return *this;
return (((*this)*(*this))^(a>>1))*((*this)^(a&1));
}
};
modint invert(modint a){
return a^(MOD-2);
}
modint operator/(modint a, modint b){
return a*invert(b);
}
unordered_map<int,int> amp;
unordered_map<int,int> vmp;
int _findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
unordered_set<int> off;
vector<int> vec,v2;
REP(i,0,N){
vec.pb(i);
}
v2=vec;
REP(i,0,M){
swap(vec[X[M-i-1]],vec[Y[M-i-1]]);
}
REP(i,0,M){
swap(vec[X[i]],vec[Y[i]]);
}
assert(v2==vec);
REP(i,0,M){
swap(vec[X[M-i-1]],vec[Y[M-i-1]]);
}
vector<int> arr,a2;
REP(i,0,N)arr.pb(S[i]);
a2=arr;
REP(i,0,M){
P[i]=Q[i]=0;
}
REP(i,0,N){
amp[arr[i]]=i;
vmp[vec[i]]=i;
}
unordered_map<int,int> va;
vector<int> corr;
REP(i,0,N){
va[vec[i]]=arr[i];
}
int rounds=0;
REP(i,0,N){
if(va[i]==i)continue;
vector<int> loop;
int x=i;
do{
loop.pb(x);
x=va[x];
}while(x!=i);
rounds+=loop.size()-1;
FORJ(loop)va[j]=j;
}
int _M=M;
arr=a2;
vec=v2;
REP(i,0,M){
swap(vec[X[M-i-1]],vec[Y[M-i-1]]);
}
REP(i,0,N){
amp[arr[i]]=i;
vmp[vec[i]]=i;
}
REP(i,0,N){
if(arr[i]!=vec[i])off.insert(arr[i]);
}
REP(i,0,M){
if(!off.size())break;
int v=Y[i];
int a=X[i];
amp[arr[a]]=-1;
amp[arr[v]]=-1;
vmp[vec[a]]=-1;
vmp[vec[v]]=-1;
swap(arr[a],arr[v]);
swap(vec[a],vec[v]);
amp[arr[a]]=a;
amp[arr[v]]=v;
vmp[vec[a]]=a;
vmp[vec[v]]=v;
if(arr[a]==vec[a])off.erase(arr[a]);
if(arr[v]==vec[v])off.erase(arr[v]);
if(!off.size())break;
auto x=*off.begin();
a=amp[x];
v=vmp[x];
amp[arr[a]]=-1;
amp[arr[v]]=-1;
vmp[vec[a]]=-1;
vmp[vec[v]]=-1;
swap(arr[a],arr[v]);
amp[arr[a]]=a;
amp[arr[v]]=v;
vmp[vec[a]]=a;
vmp[vec[v]]=v;
if(arr[a]==vec[a])off.erase(arr[a]);
if(arr[v]==vec[v])off.erase(arr[v]);
P[i]=a;
Q[i]=v;
}
if(arr!=vec)return -1;
if(off.size())return 1;
SORT(arr);
if(a2==arr)return 0;
REP(i,0,_M){
swap(a2[X[i]],a2[Y[i]]);
swap(a2[P[i]],a2[Q[i]]);
if(a2==arr)return i+1;
}
if(a2==arr)return M;
return -1;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l=0,r=M;
int ans=M;
assert(_findSwapPairs(N,S,M,X,Y,P,Q)!=-1);
while(l<=r){
int m=(l+r)/2;
if(_findSwapPairs(N,S,m,X,Y,P,Q)==-1)l=m+1;
else r=m-1,ans=m;
}
return _findSwapPairs(N,S,ans,X,Y,P,Q);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |