#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);
}
map<int,int> amp;
map<int,int> vmp;
set<int> off;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
vector<int> vec;
REP(i,0,N){
vec.pb(i);
}
REP(i,0,M){
swap(vec[X[M-i-1]],vec[Y[M-i-1]]);
}
vector<int> arr;
REP(i,0,N)arr.pb(S[i]);
#pragma GCC poison S
REP(i,0,M){
X[i]=Y[i]=0;
}
REP(i,0,N){
if(arr[i]!=vec[i])off.insert(vec[i]);
}
REP(i,0,N){
amp[arr[i]]=i;
vmp[vec[i]]=i;
}
REP(i,0,M){
if(arr==vec)break;
assert(off.size());
auto x=*off.begin();
int a=amp[x];
int 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;
P[i]=a;
Q[i]=v;
if(arr[a]==vec[a])off.erase(arr[a]);
if(arr[v]==vec[v])off.erase(arr[v]);
a=X[i];
v=Y[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]);
}
assert(arr==vec);
assert(!off.size());
return M;
}
# | 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... |