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>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int findSwapPairs(int n, int arr[], int m, int X[], int Y[], int P[], int Q[])
{
auto chk = [&](int val, bool f = false) -> bool
{
vector<int>finalpos(n, 0), cur(n, 0);
for(int i = 0; i < n; i++)
cur[i] = i;
for(int i = 0; i < val - 1; i++)
swap(cur[X[i]], cur[Y[i]]);
for(int i = 0; i < n; i++)
finalpos[cur[i]] = i;
vector<int>where(n), v(n);
for(int i = 0; i < n; i++)
{
v[i] = arr[i];
where[v[i]] = i;
}
int sw = 0;
for(int i = 0; i < n; i++)
{
if(finalpos[i] != v[i])
{
if(sw >= val)
return false;
int pos = where[finalpos[i]];
//cout << i << " " << pos << endl;
int was = v[i];
swap(v[pos], v[i]);
if(f)
cout << pos << " " << i << endl;
P[sw] = pos, Q[sw] = i;
swap(where[finalpos[i]], where[was]);
if(f)
{
cout << "after swap 1: " << endl;
for(int i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
}
if(sw < val - 1)
{
swap(finalpos[X[sw]], finalpos[Y[sw]]);
swap(v[X[sw]], v[Y[sw]]);
swap(where[v[X[sw]]], where[v[Y[sw]]]);
}
if(f)
{
cout << "after swap 2: " << endl;
for(int i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
cout << "where array: " << endl;
for(int i = 0; i < n; i++)
cout << where[i] << " ";
cout << endl;
}
++sw;
--i;
}
}
return true;
};
int l = 0, r = m;
while(l < r)
{
int mid = (l + r) / 2;
if(chk(mid))
r = mid;
else
l = mid + 1;
}
//chk(l, true);
return l;
}
Compilation message (stderr)
sorting.cpp: In lambda function:
sorting.cpp:54:14: warning: declaration of 'i' shadows a previous local [-Wshadow]
for(int i = 0; i < n; i++)
^
sorting.cpp:37:11: note: shadowed declaration is here
for(int i = 0; i < n; i++)
^
sorting.cpp:67:14: warning: declaration of 'i' shadows a previous local [-Wshadow]
for(int i = 0; i < n; i++)
^
sorting.cpp:37:11: note: shadowed declaration is here
for(int i = 0; i < n; i++)
^
sorting.cpp:71:14: warning: declaration of 'i' shadows a previous local [-Wshadow]
for(int i = 0; i < n; i++)
^
sorting.cpp:37:11: note: shadowed declaration is here
for(int i = 0; i < n; i++)
^
# | 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... |