#include "sorting.h"
#include <bits/stdc++.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define per(i,a,b) for(int i = a; i >= b; i--)
#define inf 9223372036854775807
#define all(x) x.begin(),x.end()
#define sz(x) (ll)(x).size()
#define trav(a,x) for(auto& a : x)
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
ll lo = -1, hi = N, mid;
while (lo+1 < hi){
mid = (lo+hi)/2;
vi last; last.assign(N,0);
rep(i,0,N) last[i] = S[i];
rep(i,0,mid){
ll t1 = last[X[i]];
ll t2 = last[Y[i]];
last[Y[i]] = t1;
last[X[i]] = t2;
}
vi inv; inv.assign(N,0);
rep(i,0,N) inv[last[i]] = i;
ll cur = 0;
rep(i,0,mid){
while (cur < N && inv[cur] == cur) cur++;
if (cur == N) break;
ll s = last[cur],ls = inv[cur];
last[ls] = s;
last[cur] = cur;
inv[cur] = cur;
inv[s] = ls;
}
bool check = true;
rep(i,0,N) if (inv[i] != i) check = false;
if (check) {
hi = mid;
} else {
lo = mid;
}
}
if (hi == 0){
bool check = true;
rep(i,0,N) if (S[i] != i) check = false;
if (check) return 0;
else hi = 1;
}
vi last; last.assign(N,0);
rep(i,0,N) last[i] = S[i];
rep(i,0,hi){
ll t1 = last[X[i]];
ll t2 = last[Y[i]];
last[Y[i]] = t1;
last[X[i]] = t2;
}
vi inv; inv.assign(N,0);
rep(i,0,N) inv[last[i]] = i;
ll cur = 0;
vii swap;
rep(i,0,hi){
while (cur < N && inv[cur] == cur) cur++;
if (cur == N) break;
ll s = last[cur],ls = inv[cur];
swap.push_back(make_pair(s,cur));
last[ls] = s;
last[cur] = cur;
inv[cur] = cur;
inv[s] = ls;
}
while (sz(swap) < hi) swap.push_back(make_pair(0,0));
last.assign(N,0);
inv.assign(N,0);
rep(i,0,N) last[i] = S[i];
rep(i,0,N) inv[last[i]] = i;
rep(i,0,hi){
ll t1 = last[X[i]];
ll t2 = last[Y[i]];
last[Y[i]] = t1;
last[X[i]] = t2;
inv[t1] = Y[i];
inv[t2] = X[i];
//
P[i] = inv[swap[i].first];
Q[i] = inv[swap[i].second];
last[P[i]] = swap[i].second;
last[Q[i]] = swap[i].first;
inv[swap[i].first] = Q[i];
inv[swap[i].second] = P[i];
}
return hi;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:92:29: warning: conversion to 'int' from '__gnu_cxx::__alloc_traits<std::allocator<long long int> >::value_type {aka long long int}' may alter its value [-Wconversion]
P[i] = inv[swap[i].first];
^
sorting.cpp:93:30: warning: conversion to 'int' from '__gnu_cxx::__alloc_traits<std::allocator<long long int> >::value_type {aka long long int}' may alter its value [-Wconversion]
Q[i] = inv[swap[i].second];
^
sorting.cpp:99:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return hi;
^~
sorting.cpp:19:39: warning: unused parameter 'M' [-Wunused-parameter]
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
432 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
227 ms |
14168 KB |
Output is correct |
16 |
Correct |
232 ms |
14524 KB |
Output is correct |
17 |
Correct |
356 ms |
15092 KB |
Output is correct |
18 |
Correct |
91 ms |
8388 KB |
Output is correct |
19 |
Correct |
264 ms |
13112 KB |
Output is correct |
20 |
Correct |
340 ms |
13376 KB |
Output is correct |
21 |
Correct |
289 ms |
13480 KB |
Output is correct |
22 |
Correct |
346 ms |
15336 KB |
Output is correct |
23 |
Correct |
247 ms |
15472 KB |
Output is correct |
24 |
Correct |
369 ms |
15332 KB |
Output is correct |
25 |
Correct |
280 ms |
15236 KB |
Output is correct |
26 |
Correct |
191 ms |
13392 KB |
Output is correct |
27 |
Correct |
225 ms |
12872 KB |
Output is correct |
28 |
Correct |
377 ms |
15064 KB |
Output is correct |
29 |
Correct |
343 ms |
14964 KB |
Output is correct |
30 |
Correct |
179 ms |
11116 KB |
Output is correct |
31 |
Correct |
335 ms |
14932 KB |
Output is correct |
32 |
Correct |
362 ms |
15064 KB |
Output is correct |
33 |
Correct |
332 ms |
15404 KB |
Output is correct |
34 |
Correct |
241 ms |
15304 KB |
Output is correct |
35 |
Correct |
266 ms |
12860 KB |
Output is correct |
36 |
Correct |
88 ms |
9020 KB |
Output is correct |
37 |
Correct |
458 ms |
15732 KB |
Output is correct |
38 |
Correct |
460 ms |
15156 KB |
Output is correct |
39 |
Correct |
390 ms |
15180 KB |
Output is correct |