#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 a=i;
while(!vis[a])
{
vis[a]=1;
if(!vis[f[a]]) v.push_back({a,f[a]});
a=f[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:27:22: warning: conversion from 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
27 | int sz=v.size();
| ~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |