#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
typedef int_fast32_t ll;
#define SWAP(x, y) t = (x); (x) = (y); (y) = (x);
ll t;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P1[], int Q1[]) {
ll h[200000];
for (ll i = 0; i < N; i++) h[i] = S[i];
ll ooo = 0;
for (ll i = 0; i < N; i++) {
if (h[i] != i) ooo++;
}
if (ooo == 0) return 0;
int P[200000], Q[200000]; //
ll revH[200000];
for (int i = 0; i < N; i++) {
revH[h[i]] = i;
}
ll res1 =0;
ll low = 1, high = N;
ll s[200000], order[200000], revOrd[200000], revS[200000];
while (low < high) {
ll g = (low + high) / 2;
ll res = 0;
memcpy(s, h, N * sizeof(ll));
memcpy(order, h, N * sizeof(ll));
memcpy(revOrd, revH, N * sizeof(ll));
memcpy(revS, revH, N * sizeof(ll));
for (ll j = 0; j < g; j++) {
SWAP(order[X[j]], order[Y[j]]);
SWAP(revOrd[order[X[j]]], revOrd[order[Y[j]]]);
}
ll id1 = -1;
for (ll i = 0; i < g; i++) {
SWAP(s[X[i]], s[Y[i]]);
SWAP(revS[s[X[i]]], revS[s[Y[i]]]);
int firstOut = -1, id = -1;
for (int j = id1+1; j < N; j++) {
if (order[j] != j) {
firstOut = order[id = j];
break;
}
}
id1 = id;
if (firstOut == -1) {
P[res] = Q[res] = 0;
res++;
}
else {
ll iter = revOrd[id];
ll v = order[iter];
ll id1 = revS[firstOut];
ll id2 = revS[v];
SWAP(s[id1], s[id2]);
SWAP(revS[s[id1]], revS[s[id2]]);
SWAP(order[iter], order[id]);
SWAP(revOrd[order[iter]], revOrd[order[id]]);
P[res] = id1;
Q[res] = id2;
res++;
}
}
ll ooo = 0;
for (ll i = 0; i < N; i++) {
if (s[i] != i) ooo++;
}
if (ooo == 0) {
res1 = res;
high = res;
memcpy(P1, P, res * sizeof(int));
memcpy(Q1, Q, res * sizeof(int));
}else {
low = g+1;
}
}
return res1;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:52:29: warning: conversion from 'll' {aka 'long int'} to 'int' may change value [-Wconversion]
52 | for (int j = id1+1; j < N; j++) {
| ~~~^~
sorting.cpp:54:44: warning: conversion from 'll' {aka 'long int'} to 'int' may change value [-Wconversion]
54 | firstOut = order[id = j];
| ~~~~~~~~~~~~^
sorting.cpp:67:20: warning: declaration of 'id1' shadows a previous local [-Wshadow]
67 | ll id1 = revS[firstOut];
| ^~~
sorting.cpp:47:12: note: shadowed declaration is here
47 | ll id1 = -1;
| ^~~
sorting.cpp:73:26: warning: conversion from 'll' {aka 'long int'} to 'int' may change value [-Wconversion]
73 | P[res] = id1;
| ^~~
sorting.cpp:74:26: warning: conversion from 'll' {aka 'long int'} to 'int' may change value [-Wconversion]
74 | Q[res] = id2;
| ^~~
sorting.cpp:78:12: warning: declaration of 'ooo' shadows a previous local [-Wshadow]
78 | ll ooo = 0;
| ^~~
sorting.cpp:18:8: note: shadowed declaration is here
18 | ll ooo = 0;
| ^~~
sorting.cpp:92:9: warning: conversion from 'll' {aka 'long int'} to 'int' may change value [-Wconversion]
92 | return res1;
| ^~~~
sorting.cpp:14:39: warning: unused parameter 'M' [-Wunused-parameter]
14 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P1[], int Q1[]) {
| ~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11356 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Incorrect |
5 ms |
11352 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11356 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Incorrect |
5 ms |
11352 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11356 KB |
Output is correct |
2 |
Incorrect |
5 ms |
11356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11356 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Incorrect |
5 ms |
11352 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
11356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
11356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |