# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
318015 | wildturtle | The Big Prize (IOI17_prize) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
int a,le,ri,ans,mid;
vector <int> S1,A;
vector < pair < int , int > > v;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
le=0; ri=M; ans=-1;
while(le<=ri) {
mid=(le+ri)/2;
for(int i=0;i<N;i++)
S1[i]=S[i];
for(int i=0;i<mid;i++) {
swap(S1[X[i]],S1[Y[i]]);
}
for(int i=0;i<N;i++) {
A[S1[i]]=i;
}
v.clear();
for(int i=0;i<N;i++) {
if(S1[i]==i) continue;
v.push_back({i,S1[i]});
swap(S1[i],S1[A[i]]);
swap(A[i],A[S1[A[i]]]);
}
a=v.size();
if(a<=mid) {
for(int i=0;i<N;i++) {
S1[i]=S[i];
A[S1[i]]=i;
}
for(int i=0;i<M;i++) {
swap(A[S1[X[i]]],A[S1[Y[i]]]);
swap(S1[X[i]],S1[Y[i]]);
if(i<a) {
P[i]=A[v[i].first];
Q[i]=A[v[i].second];
swap(A[v[i].first],A[v[i].second]);
swap(S1[P[i]],S1[Q[i]]);
}
else { P[i]=0; Q[i]=0; }
}
ri=mid-1;
ans=mid;
}
else le=mid+1;
}
return ans;
}