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 "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int a[200005], b[200005], c[200005], d[200005];
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[])
{
int l=1, r=m, mid, cur;
while (l<r)
{
mid=(l+r)/2;
for (int i=0; i<n; i++)
{
a[i]=b[i]=i;
c[i]=s[i];
d[s[i]]=i;
}
for (int i=mid-1; i>=0; i--)
{
swap(a[x[i]], a[y[i]]);
swap(b[a[x[i]]], b[a[y[i]]]);
}
cur=0;
for (int i=0; i<mid; i++)
{
swap(a[x[i]], a[y[i]]);
swap(b[a[x[i]]], b[a[y[i]]]);
swap(c[x[i]], c[y[i]]);
swap(d[c[x[i]]], d[c[y[i]]]);
while (cur<=n && b[cur]==d[cur])
cur++;
if (cur<n)
{
swap(c[b[cur]], c[d[cur]]);
swap(d[c[b[cur]]], d[c[d[cur]]]);
while (cur<=n && b[cur]==d[cur])
cur++;
}
}
if (cur<n)
l=mid+1;
else
r=mid;
}
for (int i=0; i<n; i++)
{
a[i]=b[i]=i;
c[i]=s[i];
d[s[i]]=i;
}
for (int i=l-1; i>=0; i--)
{
swap(a[x[i]], a[y[i]]);
swap(b[a[x[i]]], b[a[y[i]]]);
}
cur=0;
for (int i=0; i<l; i++)
{
swap(a[x[i]], a[y[i]]);
swap(b[a[x[i]]], b[a[y[i]]]);
swap(c[x[i]], c[y[i]]);
swap(d[c[x[i]]], d[c[y[i]]]);
while (cur<=n && b[cur]==d[cur])
cur++;
if (cur<n)
{
p[i]=b[cur];
q[i]=d[cur];
swap(c[b[cur]], c[d[cur]]);
swap(d[c[b[cur]]], d[c[d[cur]]]);
while (cur<=n && b[cur]==d[cur])
cur++;
}
else
p[i]=q[i]=0;
}
return l;
}
# | 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... |