#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 2e5 + 10 , M = 6e5 + 10;
int n , ar[N] , pos[N] , m , L[M] , R[M] , f[M] , s[M];
vector <pair<int , int>> vec;
bool Check(int mid)
{
for(int i = 0 ; i < mid ; i++)
swap(ar[L[i]] , ar[R[i]]);
for(int i = 0 ; i < n ; i++)
pos[ar[i]] = i;
vec.clear();
for(int i = 0 ; i < n ; i++) if(ar[i] != i)
{
int p = pos[i];
swap(ar[i] , ar[p]);
pos[ar[p]] = p;
pos[ar[i]] = i;
vec.push_back(make_pair(ar[i] , ar[p]));
}
return (vec.size() <= mid);
}
void Solve(int mid)
{
for(int i = 0 ; i < mid ; i++)
{
swap(ar[L[i]] , ar[R[i]]);
swap(pos[ar[L[i]]] , pos[ar[R[i]]]);
f[i] = pos[vec[i].F];
s[i] = pos[vec[i].S];
swap(ar[f[i]] , ar[s[i]]);
swap(pos[ar[f[i]]] , pos[ar[s[i]]]);
}
}
int findSwapPairs(int nn, int S[], int mm, int X[], int Y[], int P[], int Q[]) {
n = nn;
m = mm;
for(int i = 0 ; i < m ; i++)
{
L[i] = X[i];
R[i] = Y[i];
}
int low = -1 , high = m;
while(high - low > 1)
{
for(int i = 0 ; i < n ; i++)
{
ar[i] = S[i];
pos[ar[i]] = i;
}
int mid = (low + high) >> 1;
if(Check(mid))
high = mid;
else
low = mid;
}
for(int i = 0 ; i < n ; i++)
{
ar[i] = S[i];
pos[ar[i]] = i;
}
Check(high);
for(int i = 0 ; i < n ; i++)
{
ar[i] = S[i];
pos[ar[i]] = i;
}
Solve(high);
for(int i = 0 ; i < m ; i++)
{
P[i] = f[i];
Q[i] = s[i];
}
return high;
}
Compilation message
sorting.cpp: In function 'bool Check(int)':
sorting.cpp:28:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | return (vec.size() <= mid);
| ~~~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |