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 <bits/stdc++.h>
#include "sorting.h"
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
typedef long long ll;
using namespace std;
const int maxN = 2e5 + 10, maxM = 3 * maxN;
int x[maxM], y[maxM];
int n, q[maxN], tmp[maxN], p[maxN];
bool mark[maxN];
vector <int> a1 ,a2;
int check(int m, bool f = 0)
{
for (int i=0; i<m; i++)
{
int a = p[x[i]], b = p[y[i]];
p[x[i]] = b, p[y[i]] = a;
}
memset(mark, 0, sizeof mark);
int ted = 0;
vector <pii > swaps;
for (int i=0; i<n; i++)
{
if(mark[i]) continue ;
vector <int> vc;
ted ++;
int curr = i;
while(!mark[curr])
{
mark[curr] = true;
if(!mark[p[curr]]) swaps.pb({curr, p[curr]});
curr = p[curr];
}
}
if(ted < n - m) return 0;
else if(!f) return 1;
for (int i=0; i<n; i++) p[i] = tmp[i], q[p[i]] = i;
for (int i=0; i<m; i++)
{
int a = p[x[i]], b = p[y[i]];
p[x[i]] = b, p[y[i]] = a;
q[b] = x[i], q[a] = y[i];
if(i >= swaps.size()) continue ;
int v1 = swaps[i].F, v2 = swaps[i].S;
int xi = q[v1], yi = q[v2];
a = p[xi], b = p[yi];
p[xi] = b, p[yi] = a;
q[b] = xi, q[a] = yi;
a1.pb(xi); a2.pb(yi);
}
return 1;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
int lo = -1, hi = M;
for (int i=0; i<M; i++) x[i] = X[i], y[i] = Y[i];
for (int i=0; i<n; i++) tmp[i] = S[i];
while(hi - lo > 1)
{
for (int i=0; i<n; i++) p[i] = S[i];
int mid = (lo + hi)/2;
if(check(mid)) hi = mid;
else lo = mid;
}
for (int i=0; i<n; i++) p[i] = S[i];
check(hi, 1);
for (int i=0; i<hi; i++) P[i] = a1[i], Q[i] = a2[i];
return hi;
}
Compilation message (stderr)
sorting.cpp: In function 'int check(int, bool)':
sorting.cpp:58:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i >= swaps.size()) continue ;
~~^~~~~~~~~~~~~~~
# | 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... |