#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
vector<int> v,v2,st,e,l,r,ls,rs;
int n,rt;
bool work(int mid){
l.clear(),r.clear(),ls.clear(),rs.clear();
v2 = v;
for(int i = 0 ; i < mid ; i += 1){
swap(v2[st[i]],v2[e[i]]);
}
int num = 0;
for(int i = 0 ; i < n ; i += 1){
while(v2[i] != i){
l.push_back(v2[i]) , r.push_back(v2[v2[i]]);
swap(v2[i],v2[v2[i]]);
num += 1;
}
}
while(num < mid){
l.push_back(0) , r.push_back(0);
num += 1;
}
if(num > mid){
return false;
}
vector<int> ind(n);
v2 = v;
for(int i = 0 ; i < n ; i += 1){
ind[v2[i]] = i;
}
for(int i = 0 ; i < mid ; i += 1){
int x = st[i] , y = e[i];
swap(ind[v2[x]],ind[v2[y]]);
swap(v2[x],v2[y]);
x = l[i] , y = r[i];
ls.push_back(ind[x]);
rs.push_back(ind[y]);
swap(v2[ind[x]],v2[ind[y]]);
swap(ind[x],ind[y]);
}
return true;
}
int findSwapPairs(int N , int s[] , int m , int x[] , int y[] , int p[] , int q[]){
n = N;
for(int i = 0 ; i < n ; i += 1){
v.push_back(s[i]);
}
for(int i = 0 ; i < m ; i += 1){
st.push_back(x[i]) , e.push_back(y[i]);
}
int first = -1 , last = m;
while(last-first > 1){
int mid = (first+last)/2;
if(work(mid)){
last = mid;
}else{
first = mid;
}
}
for(int i = 0 ; i < (int)ls.size() ; i += 1){
p[i] = ls[i] , q[i] = rs[i];
}
return last;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |