#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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |