#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int _N = 2e5 * 3;
int n, m;
int s[_N], _s[_N];
int x[_N], y[_N];
int t;
int viz[_N];
int Nxt[_N];
int pos[_N];
int rep[_N];
vector<pair<int, int>> ans, ans2;
void solve(int X)
{
map<int, int> vis;
for (int i = 0; i < n; ++i)
{
if (vis[i])
continue;
int cur = Nxt[i];
while (cur != i)
{
++vis[cur];
ans.push_back({i, cur});
cur = Nxt[cur];
}
++vis[cur];
}
while (ans.size() < X)
ans.push_back({0, 0});
ans2.resize(X);
for (int i = 0; i < n; ++i)
pos[s[i]] = i;
for (int i = 0; i < X; ++i)
{
int x1 = pos[_s[x[i]]];
int x2 = pos[_s[y[i]]];
swap(rep[x1], rep[x2]);
ans2[i] = {rep[ans[i].first], rep[ans[i].second]};
swap(_s[x[i]], _s[y[i]]);
}
}
bool can(int mid)
{
int nxt[_N];
for (int i = 0; i < n; ++i)
nxt[i] = _s[i];
for (int j = 0; j < mid; ++j)
swap(nxt[x[j]], nxt[y[j]]);
++t;
int ret = 0;
for (int i = 0; i < n; ++i)
{
int cur = nxt[i];
if (viz[i] == t)
continue;
++ret;
while (cur != i)
{
viz[cur] = t;
cur = nxt[cur];
}
viz[cur] = t;
}
return n - ret <= mid;
}
int binary_sr4()
{
int ret = m;
int lo = 1, hi = m;
while (lo <= hi)
{
int mid = (lo + hi) >> 1;
if (can(mid))
hi = mid - 1, ret = mid;
else
lo = mid + 1;
}
return ret;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n = N;
m = M;
for (int i = 0; i < N; ++i)
{
Nxt[i] = S[i];
s[i] = S[i];
_s[i] = S[i];
rep[i] = i;
}
for (int i = 0; i < M; ++i)
{
x[i] = X[i];
y[i] = Y[i];
}
if (can(0))
return 0;
int opt = binary_sr4();
for (int i = 0; i < M; ++i)
{
swap(s[X[i]], s[Y[i]]);
swap(rep[X[i]], rep[Y[i]]);
swap(Nxt[X[i]], Nxt[Y[i]]);
if (i == opt - 1)
{
solve(i + 1);
for (int j = 0; j <= i; ++j)
{
P[j] = ans2[j].first;
Q[j] = ans2[j].second;
}
return ++i;
}
}
return 0;
}
Compilation message
sorting.cpp: In function 'void solve(int)':
sorting.cpp:34:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | while (ans.size() < X)
| ~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
672 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
298 ms |
28392 KB |
Output is correct |
16 |
Correct |
321 ms |
32200 KB |
Output is correct |
17 |
Correct |
342 ms |
34152 KB |
Output is correct |
18 |
Correct |
48 ms |
17400 KB |
Output is correct |
19 |
Correct |
243 ms |
31596 KB |
Output is correct |
20 |
Correct |
252 ms |
32452 KB |
Output is correct |
21 |
Correct |
262 ms |
32492 KB |
Output is correct |
22 |
Correct |
312 ms |
34280 KB |
Output is correct |
23 |
Correct |
336 ms |
34792 KB |
Output is correct |
24 |
Correct |
356 ms |
34536 KB |
Output is correct |
25 |
Correct |
350 ms |
34536 KB |
Output is correct |
26 |
Correct |
256 ms |
32368 KB |
Output is correct |
27 |
Correct |
222 ms |
31292 KB |
Output is correct |
28 |
Correct |
344 ms |
34028 KB |
Output is correct |
29 |
Correct |
325 ms |
33780 KB |
Output is correct |
30 |
Correct |
182 ms |
30300 KB |
Output is correct |
31 |
Correct |
333 ms |
34540 KB |
Output is correct |
32 |
Correct |
335 ms |
33788 KB |
Output is correct |
33 |
Correct |
355 ms |
34792 KB |
Output is correct |
34 |
Correct |
340 ms |
34408 KB |
Output is correct |
35 |
Correct |
246 ms |
34028 KB |
Output is correct |
36 |
Correct |
128 ms |
32888 KB |
Output is correct |
37 |
Correct |
369 ms |
40296 KB |
Output is correct |
38 |
Correct |
353 ms |
38632 KB |
Output is correct |
39 |
Correct |
355 ms |
39276 KB |
Output is correct |