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 ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
// 0 1 2 0 1 2
// 0 2
// 0 1 0 1
// 1 2
// 1 2 0 1 2 0
vector<pii> ans;
void dfs(int u, vector<int> &p, vector<int> &vis)
{
vis[u] = 1;
if (!vis[p[u]])
{
ans.emplace_back(pii(u, p[u]));
dfs(p[u], p, vis);
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
auto check = [&](int K)
{
vector<int> v(N), vis(N, 0), pos(N);
ans.clear();
for (int i = 0; i < N; i++)
v[i] = S[i];
for (int i = 0; i < K; i++)
swap(v[X[i]], v[Y[i]]);
for (int i = 0; i < N; i++)
if (!vis[i])
dfs(i, v, vis);
if(ans.size() <= K)
{
reverse(ans.begin(), ans.end());
while(ans.size() <= K)
ans.emplace_back(pii(0, 0));
for(int i = 0; i < N; i++)
pos[i] = i, v[i] = i;
for(int i = K - 1; i >= 0; i--)
{
P[i] = pos[ans[i].F];
Q[i] = pos[ans[i].S];
tie(v[X[i]], v[Y[i]], pos[v[X[i]]], pos[v[Y[i]]]) = make_tuple(v[Y[i]], v[X[i]], pos[v[Y[i]]], pos[v[X[i]]]);
}
return true;
}
else
return false;
};
int L = 0, R = M;
while(L < R)
{
int mid = (L + R) / 2;
if(check(mid))
R = mid;
else
L = mid + 1;
}
check(L);
return L;
}
Compilation message (stderr)
sorting.cpp: In lambda function:
sorting.cpp:41:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
41 | if(ans.size() <= K)
| ~~~~~~~~~~~^~~~
sorting.cpp:44:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | while(ans.size() <= K)
| ~~~~~~~~~~~^~~~
# | 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... |