#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int MAX = 200010;
using pii = pair<int, int>;
#define F first
#define S second
int n, m;
int *A, *X, *Y, B[MAX];
bool vis[MAX];
vector<pii> check(int len)
{
for (int i = 0; i < n; ++i)
B[i] = A[i], vis[i] = false;
for (int i = 0; i < len; ++i)
swap(B[X[i]], B[Y[i]]);
vector<pii> ans;
for (int s = 0; s < n; ++s) {
if (vis[s]) continue;
int u = s;
while (true) {
vis[u] = true;
int v = B[u];
if (!vis[v])
ans.push_back({B[u], B[v]});
else
break;
u = v;
}
}
return ans;
}
int findSwapPairs(int N, int S[], int M, int BX[], int BY[], int P[], int Q[])
{
n = N, m = M;
A = S, X = BX, Y = BY;
int b = 0;
int e = m;
while (b < e) {
int x = (b+e)/2;
if (check(x).size() <= x)
e = x;
else
b = x+1;
}
vector<pii> way = check(b);
reverse(way.begin(), way.end());
for (int i = 0; i < n; ++i)
B[A[i]] = i;
for (int i = 0; i < way.size(); ++i) {
swap(B[A[X[i]]], B[A[Y[i]]]);
swap(A[X[i]], A[Y[i]]);
int x = way[way.size()-i-1].F, y = way[way.size()-i-1].S;
P[i] = B[x], Q[i] = B[y];
swap(A[B[x]], A[B[y]]);
swap(B[x], B[y]);
}
return b;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:47:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (check(x).size() <= x)
~~~~~~~~~~~~~~~~^~~~
sorting.cpp:56:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < way.size(); ++i) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 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 |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 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 |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
4 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 |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 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 |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 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 |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
274 ms |
11012 KB |
Output is correct |
16 |
Correct |
248 ms |
11076 KB |
Output is correct |
17 |
Correct |
283 ms |
11632 KB |
Output is correct |
18 |
Correct |
79 ms |
9368 KB |
Output is correct |
19 |
Correct |
153 ms |
10476 KB |
Output is correct |
20 |
Correct |
175 ms |
10800 KB |
Output is correct |
21 |
Correct |
201 ms |
10908 KB |
Output is correct |
22 |
Correct |
258 ms |
11628 KB |
Output is correct |
23 |
Correct |
326 ms |
11988 KB |
Output is correct |
24 |
Correct |
327 ms |
11712 KB |
Output is correct |
25 |
Correct |
280 ms |
11748 KB |
Output is correct |
26 |
Correct |
206 ms |
9488 KB |
Output is correct |
27 |
Correct |
173 ms |
9000 KB |
Output is correct |
28 |
Correct |
335 ms |
11656 KB |
Output is correct |
29 |
Correct |
295 ms |
11336 KB |
Output is correct |
30 |
Correct |
103 ms |
8044 KB |
Output is correct |
31 |
Correct |
264 ms |
11528 KB |
Output is correct |
32 |
Correct |
266 ms |
11604 KB |
Output is correct |
33 |
Correct |
360 ms |
11812 KB |
Output is correct |
34 |
Correct |
329 ms |
11880 KB |
Output is correct |
35 |
Correct |
172 ms |
10392 KB |
Output is correct |
36 |
Correct |
50 ms |
6776 KB |
Output is correct |
37 |
Correct |
325 ms |
11984 KB |
Output is correct |
38 |
Correct |
356 ms |
11664 KB |
Output is correct |
39 |
Correct |
274 ms |
11684 KB |
Output is correct |