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>
using namespace std;
int n,m,*x,*y,*s,*p,*q;
const long long MX=2e5+10;
int a[MX],ai[MX];
int b[MX],bi[MX];
bool f(int k)
{
for(int i=0;i<n;i++)
{
a[i]=s[i];
b[i]=s[i];
}
for(int i=0;i<k;i++)
{
swap(a[x[i]],a[y[i]]);
}
for(int i=0;i<n;i++)
{
ai[a[i]]=i;
bi[b[i]]=i;
}
int uk=0;
for(int i=0;i<k;i++)
{
swap(bi[b[x[i]]],bi[b[y[i]]]);
swap(b[x[i]],b[y[i]]);
while(uk<n && a[uk]==uk)
{
uk++;
}
if(uk==n)
{
p[i]=0;
q[i]=0;
continue;
}
p[i]=bi[uk];
q[i]=bi[a[uk]];
swap(b[p[i]],b[q[i]]);
swap(bi[uk],bi[a[uk]]);
ai[a[uk]]=ai[uk];
swap(a[uk],a[ai[uk]]);
ai[uk]=uk;
}
bool o=1;
for(int i=0;i<n;i++)
{
o&=(a[i]==i);
}
return o;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n=N;m=M;s=S;x=X;y=Y;p=P;q=Q;
int l=-1,r=M;
while(l+1<r)
{
int mid=(l+r)/2;
if(f(mid))
{
r=mid;
}
else
{
l=mid;
}
}
f(r);
return r;
}
# | 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... |