#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
typedef long long ll;
int findSwapPairs(int n,int s[],int m,int x[],int y[],int p[],int q[])
{
auto solve=[&](int mv)->vector<array<int,2>>
{
vector<int> f(n,-1);
for(int i=0;i<n;i++) f[i]=s[i];
for(int i=0;i<mv;i++) swap(f[x[i]],f[y[i]]);
vector<bool> vis(n,0);
vector<array<int,2>> v;
for(int i=0;i<n;i++)
{
if(vis[i]) continue;
int b=i;
int a=i;
while(!vis[a])
{
vis[a]=1;
a=f[a];
if(!vis[a]) v.push_back({b,a});
}
}
int sz=v.size();
if(sz<=mv)
{
f.assign(n,-1);
for(int i=0;i<n;i++) f[i]=s[i];
vector<int> g(n,-1);
for(int i=0;i<n;i++) g[f[i]]=i;
vector<array<int,2>> res;
for(int i=0;i<mv;i++)
{
swap(f[x[i]],f[y[i]]);
swap(g[f[x[i]]],g[f[y[i]]]);
if(i<sz) res.push_back({g[v[i][0]],g[v[i][1]]});
else res.push_back({g[0],g[0]});
}
return res;
}
else return {{-1,-1}};
};
int l=-1,r=m;
while(l<r-1)
{
int mv=(l+r)/2;
auto res=solve(mv);
if(!(res.size()==1&&res[0][0]==-1)) r=mv;
else l=mv;
}
auto res=solve(r);
for(int i=0;i<r;i++)
{
p[i]=res[i][0];
q[i]=res[i][1];
}
return r;
}
Compilation message
sorting.cpp: In lambda function:
sorting.cpp:28:22: warning: conversion from 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
28 | int sz=v.size();
| ~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
304 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
584 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
2 ms |
444 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
2 ms |
444 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
183 ms |
20852 KB |
Output is correct |
16 |
Correct |
214 ms |
23664 KB |
Output is correct |
17 |
Correct |
218 ms |
24732 KB |
Output is correct |
18 |
Correct |
97 ms |
20416 KB |
Output is correct |
19 |
Correct |
182 ms |
22692 KB |
Output is correct |
20 |
Correct |
182 ms |
23392 KB |
Output is correct |
21 |
Correct |
173 ms |
23424 KB |
Output is correct |
22 |
Correct |
167 ms |
20240 KB |
Output is correct |
23 |
Correct |
200 ms |
25536 KB |
Output is correct |
24 |
Correct |
236 ms |
25224 KB |
Output is correct |
25 |
Correct |
245 ms |
25016 KB |
Output is correct |
26 |
Correct |
179 ms |
21936 KB |
Output is correct |
27 |
Correct |
168 ms |
21012 KB |
Output is correct |
28 |
Correct |
215 ms |
25084 KB |
Output is correct |
29 |
Correct |
221 ms |
24304 KB |
Output is correct |
30 |
Correct |
138 ms |
20584 KB |
Output is correct |
31 |
Correct |
237 ms |
24836 KB |
Output is correct |
32 |
Correct |
192 ms |
24556 KB |
Output is correct |
33 |
Correct |
226 ms |
25228 KB |
Output is correct |
34 |
Correct |
202 ms |
25172 KB |
Output is correct |
35 |
Correct |
180 ms |
22436 KB |
Output is correct |
36 |
Correct |
80 ms |
20168 KB |
Output is correct |
37 |
Correct |
224 ms |
25340 KB |
Output is correct |
38 |
Correct |
217 ms |
24984 KB |
Output is correct |
39 |
Correct |
219 ms |
24432 KB |
Output is correct |