This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
int n, ss[N], m, x[N], y[N], p[N], q[N];
int tmp[N];
int solve(int M) {
for(int i = 0; i < n; i++)
tmp[i] = ss[i];
for(int i = 0; i < M; i++)
swap(tmp[x[i]], tmp[y[i]]);
int cnt = 0;
for(int i = 0; i < n; ) {
if(i == tmp[i]) { i++; continue; }
p[cnt] = tmp[i];
q[cnt] = tmp[tmp[i]];
swap(tmp[i], tmp[tmp[i]]);
cnt++;
}
if(cnt > M) return -1;
while(cnt < M) {
p[cnt] = 0;
q[cnt] = 0;
cnt++;
}
return cnt;
}
int pos[N];
int findSwapPairs(int _n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = _n, m = M;
for(int i = 0; i < n; i++)
ss[i] = S[i];
for(int i = 0; i < m; i++)
x[i] = X[i], y[i] = Y[i];
int lo = 0, hi = M, idx;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
int R = solve(mid);
if(R == -1) lo = mid+1;
else idx = mid, hi = mid-1;
}
for(int i = 0; i < n; i++)
pos[S[i]] = i;
int R = solve(idx);
for(int i = 0; i < R; i++) {
swap(pos[ss[x[i]]], pos[ss[y[i]]]);
swap(ss[x[i]], ss[y[i]]);
P[i] = pos[p[i]];
Q[i] = pos[q[i]];
swap(pos[p[i]], pos[q[i]]);
swap(ss[P[i]], ss[Q[i]]);
}
return R;
}
Compilation message (stderr)
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:49:19: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
int R = solve(idx);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |