#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 5;
int n, m;
vector<int> s, x, y;
vector<int> a, revA, en, revEn;
bool valid(int t)
{
a = s;
for (int i = 0; i < t; i++)
swap(a[x[i]], a[y[i]]);
for (int i = 0; i < n; i++)
revA[a[i]] = i;
int cnt = 0;
for (int i = 0; i < n; i++)
if (a[i] != i)
{
int j = revA[i];
swap(revA[a[i]], revA[a[j]]);
swap(a[i], a[j]);
cnt++;
}
return cnt <= t;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n = N, m = M;
s.resize(N), x.resize(N), y.resize(N), a.resize(N), revA.resize(N), en.resize(N), revEn.resize(N);
for (int i = 0; i < N; i++)
{
s.at(i) = S[i];
x.at(i) = X[i];
y.at(i) = Y[i];
}
int R = 0;
int l = 0, r = M;
while (l != r)
{
int mid = (l + r) / 2;
if (!valid(mid))
l = mid + 1;
else
r = mid;
}
R = l;
a = s;
for (int i = 0; i < n; i++)
revA[a[i]] = i;
en = a;
for (int i = 0; i < R; i++)
swap(en[x[i]], en[y[i]]);
for (int i = 0; i < n; i++)
revEn[en[i]] = i;
queue<pair<int, int>> changes;
vector<int> tmp = en;
for (int i = 0; i < n; i++)
if (tmp[i] != i)
{
int j = revEn[i];
swap(revEn[tmp[i]], revEn[tmp[j]]);
swap(tmp[i], tmp[j]);
changes.push({i, j});
}
for (int i = 0; i < R; i++)
{
swap(revA[a[x[i]]], revA[a[y[i]]]);
swap(a[x[i]], a[y[i]]);
if (!changes.empty())
{
auto change = changes.front();
changes.pop();
P[i] = revA[en[change.first]];
Q[i] = revA[en[change.second]];
swap(en[change.first], en[change.second]);
swap(revA[a[P[i]]], revA[a[Q[i]]]);
swap(a[P[i]], a[Q[i]]);
}
else
P[i] = Q[i] = 1;
}
return R;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
224 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
177 ms |
13856 KB |
Output is correct |
16 |
Correct |
184 ms |
14148 KB |
Output is correct |
17 |
Correct |
197 ms |
14852 KB |
Output is correct |
18 |
Correct |
76 ms |
11076 KB |
Output is correct |
19 |
Correct |
154 ms |
13492 KB |
Output is correct |
20 |
Correct |
166 ms |
14132 KB |
Output is correct |
21 |
Correct |
175 ms |
14208 KB |
Output is correct |
22 |
Correct |
165 ms |
15004 KB |
Output is correct |
23 |
Correct |
207 ms |
15336 KB |
Output is correct |
24 |
Correct |
236 ms |
15144 KB |
Output is correct |
25 |
Correct |
210 ms |
15016 KB |
Output is correct |
26 |
Correct |
161 ms |
14188 KB |
Output is correct |
27 |
Correct |
140 ms |
13508 KB |
Output is correct |
28 |
Correct |
229 ms |
16256 KB |
Output is correct |
29 |
Correct |
191 ms |
15656 KB |
Output is correct |
30 |
Correct |
121 ms |
12656 KB |
Output is correct |
31 |
Correct |
192 ms |
15936 KB |
Output is correct |
32 |
Correct |
210 ms |
14920 KB |
Output is correct |
33 |
Correct |
226 ms |
15200 KB |
Output is correct |
34 |
Correct |
206 ms |
15156 KB |
Output is correct |
35 |
Correct |
156 ms |
13540 KB |
Output is correct |
36 |
Correct |
73 ms |
11944 KB |
Output is correct |
37 |
Correct |
217 ms |
15572 KB |
Output is correct |
38 |
Correct |
214 ms |
14956 KB |
Output is correct |
39 |
Correct |
218 ms |
15144 KB |
Output is correct |