#include "sorting.h"
#include <iostream>
#include <algorithm>
std::pair<int, int> valuesSorting[600010];
int b[200010];
int curPlace[200010];
bool check(int n, int a[], int cur, int x[], int y[])
{
for (int i = 0; i < n; ++i)
{
a[i] = b[i];
}
for (int i = 0; i <= cur; ++i)
{
std::swap(a[x[i]], a[y[i]]);
}
int cnt = 0;
for (int i = 0; i < n; ++i)
{
while (a[i] != i)
{
int who = a[i];
std::swap(a[i], a[who]);
++cnt;
}
}
if (cnt - 1 > cur)
return 0;
else
return 1;
}
int findSwapPairs(int n, int a[], int m, int x[], int y[], int xans[], int yans[]) {
bool flag = false;
for (int i = 0; i < n; ++i)
{
curPlace[a[i]] = i;
if (a[i] != i)
flag = true;
b[i] = a[i];
}
if (!flag)
return 0;
int lowest = m;
int l = 0, r = m - 1;
while (l <= r)
{
int mid = l + (r - l) / 2;
if (check(n, a, mid, x, y))
{
lowest = std::min(lowest, mid);
r = mid - 1;
}
else
{
l = mid + 1;
}
}
for (int i = 0; i < n; ++i)
{
a[i] = b[i];
}
for (int i = 0; i <= lowest; ++i)
{
std::swap(a[x[i]], a[y[i]]);
}
int cnt = 0;
for (int i = 0; i < n; ++i)
{
while (a[i] != i)
{
valuesSorting[cnt].first = a[i];
valuesSorting[cnt].second = a[a[i]];
int who = a[i];
std::swap(a[i], a[who]);
++cnt;
}
}
for (int i = 0; i < n; ++i)
{
a[i] = b[i];
}
for (int i = 0; i <= lowest; ++i)
{
if (x[i] != y[i])
{
int val1 = a[x[i]], val2 = a[y[i]];
std::swap(a[x[i]], a[y[i]]);
curPlace[val1] = y[i];
curPlace[val2] = x[i];
}
if (i >= cnt)
{
xans[i] = 0;
yans[i] = 0;
}
else
{
int one = valuesSorting[i].first;
int two = valuesSorting[i].second;
int curOne = curPlace[one];
int curTwo = curPlace[two];
std::swap(a[curOne], a[curTwo]);
xans[i] = curOne;
yans[i] = curTwo;
curPlace[one] = curTwo;
curPlace[two] = curOne;
}
}
return lowest + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
256 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
256 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
256 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
155 ms |
10872 KB |
Output is correct |
16 |
Correct |
172 ms |
11024 KB |
Output is correct |
17 |
Correct |
183 ms |
12024 KB |
Output is correct |
18 |
Correct |
44 ms |
6980 KB |
Output is correct |
19 |
Correct |
131 ms |
9848 KB |
Output is correct |
20 |
Correct |
143 ms |
10232 KB |
Output is correct |
21 |
Correct |
143 ms |
10360 KB |
Output is correct |
22 |
Correct |
141 ms |
11768 KB |
Output is correct |
23 |
Correct |
192 ms |
12120 KB |
Output is correct |
24 |
Correct |
219 ms |
12156 KB |
Output is correct |
25 |
Correct |
239 ms |
12280 KB |
Output is correct |
26 |
Correct |
126 ms |
10240 KB |
Output is correct |
27 |
Correct |
134 ms |
9612 KB |
Output is correct |
28 |
Correct |
210 ms |
11724 KB |
Output is correct |
29 |
Correct |
216 ms |
11896 KB |
Output is correct |
30 |
Correct |
85 ms |
8696 KB |
Output is correct |
31 |
Correct |
172 ms |
12024 KB |
Output is correct |
32 |
Correct |
185 ms |
11612 KB |
Output is correct |
33 |
Correct |
222 ms |
12452 KB |
Output is correct |
34 |
Correct |
207 ms |
12152 KB |
Output is correct |
35 |
Correct |
143 ms |
9976 KB |
Output is correct |
36 |
Correct |
46 ms |
7416 KB |
Output is correct |
37 |
Correct |
237 ms |
13112 KB |
Output is correct |
38 |
Correct |
216 ms |
12356 KB |
Output is correct |
39 |
Correct |
196 ms |
12664 KB |
Output is correct |