#include "sorting.h"
#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int findSwapPairs(int n, int arr[], int m, int X[], int Y[], int P[], int Q[])
{
auto chk = [&](int val, bool f = false) -> bool
{
vector<int>finalpos(n, 0);
for(int i = 0; i < n; i++)
finalpos[i] = i;
for(int i = val - 1; i >= 0; i--)
swap(finalpos[X[i]], finalpos[Y[i]]);
vector<int>where(n), wheref(n), v(n);
for(int i = 0; i < n; i++)
{
wheref[finalpos[i]] = i;
v[i] = arr[i];
where[v[i]] = i;
}
int sw = 0;
swap(finalpos[X[sw]], finalpos[Y[sw]]);
swap(wheref[finalpos[X[sw]]], wheref[finalpos[Y[sw]]]);
swap(v[X[sw]], v[Y[sw]]);
swap(where[v[X[sw]]], where[v[Y[sw]]]);
for(int i = 0; i < n; i++)
{
if(finalpos[where[i]] != i)
{
if(sw >= val)
return false;
int pos = wheref[i];
P[sw] = where[i], Q[sw] = pos;
swap(v[pos], v[where[i]]);
swap(where[v[pos]], where[v[where[i]]]);
assert(finalpos[where[i]] == i);
++sw;
if(sw < val)
{
swap(finalpos[X[sw]], finalpos[Y[sw]]);
swap(wheref[finalpos[X[sw]]], wheref[finalpos[Y[sw]]]);
swap(v[X[sw]], v[Y[sw]]);
swap(where[v[X[sw]]], where[v[Y[sw]]]);
}
}
}
while(sw < m)
{
P[sw] = Q[sw] = 0;
++sw;
}
return true;
};
if(is_sorted(arr, arr + n))
return 0;
int l = 1, r = m;
while(l < r)
{
int mid = (l + r) / 2;
if(chk(mid))
r = mid;
else
l = mid + 1;
}
chk(l);
return l;
}
Compilation message
sorting.cpp: In lambda function:
sorting.cpp:19:35: warning: unused parameter 'f' [-Wunused-parameter]
auto chk = [&](int val, bool f = false) -> bool
^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
348 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 |
380 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 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 |
256 KB |
Output is correct |
21 |
Correct |
3 ms |
616 KB |
Output is correct |
22 |
Correct |
4 ms |
608 KB |
Output is correct |
23 |
Correct |
4 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
632 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
552 KB |
Output is correct |
6 |
Correct |
3 ms |
456 KB |
Output is correct |
7 |
Correct |
3 ms |
476 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
476 KB |
Output is correct |
13 |
Correct |
4 ms |
476 KB |
Output is correct |
14 |
Correct |
3 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
552 KB |
Output is correct |
6 |
Correct |
3 ms |
456 KB |
Output is correct |
7 |
Correct |
3 ms |
476 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
476 KB |
Output is correct |
13 |
Correct |
4 ms |
476 KB |
Output is correct |
14 |
Correct |
3 ms |
452 KB |
Output is correct |
15 |
Correct |
390 ms |
19792 KB |
Output is correct |
16 |
Correct |
460 ms |
20304 KB |
Output is correct |
17 |
Correct |
571 ms |
21328 KB |
Output is correct |
18 |
Correct |
44 ms |
13384 KB |
Output is correct |
19 |
Correct |
389 ms |
22060 KB |
Output is correct |
20 |
Correct |
396 ms |
22600 KB |
Output is correct |
21 |
Correct |
410 ms |
22572 KB |
Output is correct |
22 |
Correct |
413 ms |
16728 KB |
Output is correct |
23 |
Correct |
453 ms |
22252 KB |
Output is correct |
24 |
Correct |
567 ms |
22112 KB |
Output is correct |
25 |
Correct |
583 ms |
21768 KB |
Output is correct |
26 |
Correct |
441 ms |
22536 KB |
Output is correct |
27 |
Correct |
400 ms |
22040 KB |
Output is correct |
28 |
Correct |
553 ms |
21924 KB |
Output is correct |
29 |
Correct |
528 ms |
21788 KB |
Output is correct |
30 |
Correct |
345 ms |
22224 KB |
Output is correct |
31 |
Correct |
581 ms |
22244 KB |
Output is correct |
32 |
Correct |
443 ms |
21456 KB |
Output is correct |
33 |
Correct |
577 ms |
21880 KB |
Output is correct |
34 |
Correct |
501 ms |
21808 KB |
Output is correct |
35 |
Correct |
381 ms |
21564 KB |
Output is correct |
36 |
Correct |
151 ms |
22164 KB |
Output is correct |
37 |
Correct |
568 ms |
22556 KB |
Output is correct |
38 |
Correct |
531 ms |
21568 KB |
Output is correct |
39 |
Correct |
550 ms |
21708 KB |
Output is correct |