#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int _N = 2e5;
int n, m;
int s[_N], _s[_N];
int x[_N], y[_N];
int nxt[_N];
int t;
int viz[_N];
int Nxt[_N];
int cyc = 0;
int pos[_N];
int rep[_N];
int cycles()
{
++t;
int ret = 0;
for (int i = 0; i < n; ++i)
{
int cnt = 1;
int cur = Nxt[i];
if (viz[i] == t)
continue;
++ret;
while (cur != i)
{
viz[cur] = t;
cur = Nxt[cur];
++cnt;
}
viz[cur] = t;
}
return ret;
}
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 = 0, 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;
}
cyc = cycles();
if (n - cyc == 0)
return 0;
int opt = binary_sr4();
for (int i = 0; i < M; ++i)
{
x[i] = X[i];
y[i] = Y[i];
swap(s[X[i]], s[Y[i]]);
swap(rep[X[i]], rep[Y[i]]);
swap(Nxt[X[i]], Nxt[Y[i]]);
if (n - cycles() <= i + 1)
{
assert(abs(opt - i) < 30) ;
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:57: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]
57 | while (ans.size() < X)
| ~~~~~~~~~~~^~~
sorting.cpp: In function 'bool can(int)':
sorting.cpp:76:19: warning: declaration of 'nxt' shadows a global declaration [-Wshadow]
76 | int nxt[_N];
| ^
sorting.cpp:10:5: note: shadowed declaration is here
10 | int nxt[_N];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
640 KB |
Output is correct |
2 |
Correct |
15 ms |
640 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
640 KB |
Output is correct |
2 |
Correct |
15 ms |
640 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Runtime error |
3 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Halted |
0 ms |
0 KB |
- |