#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define ll long long
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
ll l = 0, h = M;
while(l < h){
int m = (l + h) / 2;
int s[N], slc[N];
FOR(i, 0, N - 1) s[i] = i;
for(int i = m - 1; i >= 0; i--){
swap(s[X[i]], s[Y[i]]);
}
FOR(i, 0, N - 1) slc[s[i]] = i;
queue<int> q;
int s2[N], lc[N];
FOR(i, 0, N - 1) s2[i] = S[i], lc[S[i]] = i;
FOR(i, 0, N - 1) if(slc[i] != lc[i]) q.push(i);
FOR(i, 0, m - 1){
swap(s[X[i]], s[Y[i]]), swap(slc[s[X[i]]], slc[s[Y[i]]]);
swap(s2[X[i]], s2[Y[i]]), swap(lc[s2[X[i]]], lc[s2[Y[i]]]);
while(q.size() && slc[q.front()] == lc[q.front()]){
q.pop();
}
if(q.size()){
int qi = q.front(); q.pop();
P[i] = slc[qi];
Q[i] = lc[qi];
swap(s2[P[i]], s2[Q[i]]), swap(lc[s2[P[i]]], lc[s2[Q[i]]]);
}else{
P[i] = Q[i] = 0;
}
}
FOR(i, 0, N - 1) if(s[i] != s2[i]) {
l = m + 1;
goto spot1;
}
h = m;
spot1:;
}
int m = l;
int s[N], slc[N];
FOR(i, 0, N - 1) s[i] = i;
for(int i = m - 1; i >= 0; i--){
swap(s[X[i]], s[Y[i]]);
}
FOR(i, 0, N - 1) slc[s[i]] = i;
queue<int> q;
int s2[N], lc[N];
FOR(i, 0, N - 1) s2[i] = S[i], lc[S[i]] = i;
FOR(i, 0, N - 1) if(slc[i] != lc[i]) q.push(i);
FOR(i, 0, m - 1){
swap(s[X[i]], s[Y[i]]), swap(slc[s[X[i]]], slc[s[Y[i]]]);
swap(s2[X[i]], s2[Y[i]]), swap(lc[s2[X[i]]], lc[s2[Y[i]]]);
while(q.size() && slc[q.front()] == lc[q.front()]){
q.pop();
}
if(q.size()){
int qi = q.front(); q.pop();
P[i] = slc[qi];
Q[i] = lc[qi];
swap(s2[P[i]], s2[Q[i]]), swap(lc[s2[P[i]]], lc[s2[Q[i]]]);
}else{
P[i] = Q[i] = 0;
}
}
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:12:25: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
12 | int m = (l + h) / 2;
| ~~~~~~~~^~~
sorting.cpp:14:33: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
14 | FOR(i, 0, N - 1) s[i] = i;
| ^
sorting.cpp:18:38: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
18 | FOR(i, 0, N - 1) slc[s[i]] = i;
| ^
sorting.cpp:23:51: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
23 | FOR(i, 0, N - 1) s2[i] = S[i], lc[S[i]] = i;
| ^
sorting.cpp:24:53: warning: conversion from 'long long int' to 'std::queue<int>::value_type' {aka 'int'} may change value [-Wconversion]
24 | FOR(i, 0, N - 1) if(slc[i] != lc[i]) q.push(i);
| ^
sorting.cpp:51:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
51 | int m = l;
| ^
sorting.cpp:53:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
53 | FOR(i, 0, N - 1) s[i] = i;
| ^
sorting.cpp:57:34: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
57 | FOR(i, 0, N - 1) slc[s[i]] = i;
| ^
sorting.cpp:62:47: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
62 | FOR(i, 0, N - 1) s2[i] = S[i], lc[S[i]] = i;
| ^
sorting.cpp:63:49: warning: conversion from 'long long int' to 'std::queue<int>::value_type' {aka 'int'} may change value [-Wconversion]
63 | FOR(i, 0, N - 1) if(slc[i] != lc[i]) q.push(i);
| ^
sorting.cpp:82:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
82 | return l;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
560 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
556 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
544 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
516 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
556 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
544 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
516 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
125 ms |
20244 KB |
Output is correct |
16 |
Correct |
146 ms |
20592 KB |
Output is correct |
17 |
Correct |
176 ms |
22792 KB |
Output is correct |
18 |
Correct |
63 ms |
20304 KB |
Output is correct |
19 |
Correct |
136 ms |
20964 KB |
Output is correct |
20 |
Correct |
141 ms |
22560 KB |
Output is correct |
21 |
Correct |
141 ms |
21492 KB |
Output is correct |
22 |
Correct |
129 ms |
17248 KB |
Output is correct |
23 |
Correct |
138 ms |
22604 KB |
Output is correct |
24 |
Correct |
190 ms |
22252 KB |
Output is correct |
25 |
Correct |
178 ms |
21936 KB |
Output is correct |
26 |
Correct |
140 ms |
21524 KB |
Output is correct |
27 |
Correct |
125 ms |
20976 KB |
Output is correct |
28 |
Correct |
171 ms |
22112 KB |
Output is correct |
29 |
Correct |
164 ms |
21588 KB |
Output is correct |
30 |
Correct |
113 ms |
20456 KB |
Output is correct |
31 |
Correct |
170 ms |
22128 KB |
Output is correct |
32 |
Correct |
141 ms |
21840 KB |
Output is correct |
33 |
Correct |
177 ms |
22288 KB |
Output is correct |
34 |
Correct |
166 ms |
22212 KB |
Output is correct |
35 |
Correct |
138 ms |
20584 KB |
Output is correct |
36 |
Correct |
54 ms |
19764 KB |
Output is correct |
37 |
Correct |
182 ms |
23880 KB |
Output is correct |
38 |
Correct |
180 ms |
21952 KB |
Output is correct |
39 |
Correct |
175 ms |
21940 KB |
Output is correct |