#include "sorting.h"
#include <vector>
#include <iostream>
#define MAXN 200005
using namespace std;
int N, M, goal[MAXN], to[MAXN], P2[MAXN], P[MAXN], eX[MAXN*3], eY[MAXN*3];
int at[MAXN], E[MAXN*3][2]; //which element values to swap
bool vis[MAXN];
int can(int t) {
int swaps = 0, pt = 0;
for (int i=0; i<N; i++) {goal[i]=i;}
for (int i=0; i<t; i++) {swap(goal[eX[i]], goal[eY[i]]);}
for (int i=0; i<N; i++) {vis[i]=0;}
for (int i=0; i<N; i++) {to[i]=goal[i];} //end position aim for element with value i
for (int i=0; i<N; i++) {
if(vis[i]) {continue;}
int cur = i;
swaps--;
vector<int> Elem;
while (!vis[cur]) {
Elem.push_back(P[cur]);
vis[cur]=true, swaps++;
cur = to[P[cur]];
}
while (Elem.size() > 1) {
E[pt][0]=Elem[Elem.size()-1];
E[pt][1]=Elem[Elem.size()-2];
Elem.pop_back();
Elem.pop_back();
Elem.push_back(E[pt][0]);
pt++;
}
}
return(swaps);
}
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++) {P[i]=S[i];}
for (int i=0; i<M; i++) {
eX[i]=X[i], eY[i]=Y[i];
}
int lo = -1, hi = m;
while (lo + 1 != hi) {
int mid = (lo + hi) >> 1;
if (can(mid) <= mid) {hi = mid;}
else {lo = mid;}
}
int ans = hi, swaps = can(hi);
for (int i=0; i<n; i++) {
at[S[i]]=i;
P2[i]=P[i];
}
for (int i=0; i<swaps; i++) {
swap(at[P2[X[i]]], at[P2[Y[i]]]);
swap(P2[X[i]], P2[Y[i]]);
p[i] = at[E[i][0]], q[i] = at[E[i][1]];
swap(P2[p[i]], P2[q[i]]);
swap(at[E[i][0]],at[E[i][1]]);
}
for (int i=swaps; i<ans; i++) {
p[i]=q[i]=0;
}
return(ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
640 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
25 |
Correct |
6 ms |
640 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
238 ms |
18868 KB |
Output is correct |
16 |
Correct |
258 ms |
26620 KB |
Output is correct |
17 |
Correct |
286 ms |
28768 KB |
Output is correct |
18 |
Correct |
211 ms |
23336 KB |
Output is correct |
19 |
Correct |
298 ms |
27072 KB |
Output is correct |
20 |
Correct |
327 ms |
27448 KB |
Output is correct |
21 |
Correct |
316 ms |
27636 KB |
Output is correct |
22 |
Correct |
263 ms |
23564 KB |
Output is correct |
23 |
Correct |
274 ms |
29860 KB |
Output is correct |
24 |
Correct |
284 ms |
29424 KB |
Output is correct |
25 |
Correct |
278 ms |
28956 KB |
Output is correct |
26 |
Correct |
329 ms |
26616 KB |
Output is correct |
27 |
Correct |
280 ms |
25592 KB |
Output is correct |
28 |
Correct |
330 ms |
28368 KB |
Output is correct |
29 |
Correct |
331 ms |
27512 KB |
Output is correct |
30 |
Correct |
295 ms |
24696 KB |
Output is correct |
31 |
Correct |
330 ms |
28020 KB |
Output is correct |
32 |
Correct |
280 ms |
28232 KB |
Output is correct |
33 |
Correct |
295 ms |
29452 KB |
Output is correct |
34 |
Correct |
285 ms |
28408 KB |
Output is correct |
35 |
Correct |
302 ms |
26236 KB |
Output is correct |
36 |
Correct |
209 ms |
23160 KB |
Output is correct |
37 |
Correct |
311 ms |
29368 KB |
Output is correct |
38 |
Correct |
295 ms |
28140 KB |
Output is correct |
39 |
Correct |
312 ms |
28264 KB |
Output is correct |