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 ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "Yes" << en
#define no cout << "No" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))
using namespace std;
const ll LINF = 1e18;
const int mxN = 1e6+10, INF = 2e9;
int n, m, a[mxN], idx[mxN], *s, *x, *y;
vector<vector<int> > cycles;
bool vis[mxN];
void dfs(int i){
vis[i] = 1;
cycles.back().pb(a[i]);
if (!vis[a[i]]) dfs(a[i]);
}
bool Check(int k){
memset(vis, 0, sizeof(vis));
cycles.clear();
for (int i = 0; i < n; i++)
a[i] = s[i];
for (int i = 0; i < k; i++)
swap(a[x[i]], a[y[i]]);
for (int i = 0; i < n; i++){
if (!vis[i]){
cycles.pb({});
dfs(i);
}
}
return n-cycles.size() <= k;
}
void Swap(int i, int j){
swap(idx[s[i]], idx[s[j]]);
swap(s[i], s[j]);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N; m = M;
s = S; x = X; y = Y;
int l = 0, r = m-1, ans = m;
while (l <= r){
int k = (l + r + 1)>>1;
if (Check(k)){
ans = k;
r = k - 1;
}
else l = k + 1;
}
Check(ans);
vector<array<int, 2> > v;
for (auto c : cycles)
for (int i = 0; i < c.size()-1; i++)
v.pb({c[i], c[i+1]});
for (int i = 0; i < n; i++)
idx[s[i]] = i;
for (int i = 0; i < ans; i++){
Swap(x[i], y[i]);
if (i < v.size()){
P[i] = idx[v[i][0]];
Q[i] = idx[v[i][1]];
}
else P[i] = Q[i] = 0;
Swap(P[i], Q[i]);
}
return ans;
}
Compilation message (stderr)
sorting.cpp: In function 'bool Check(int)':
sorting.cpp:60:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | return n-cycles.size() <= k;
| ~~~~~~~~~~~~~~~~^~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < c.size()-1; i++)
| ~~^~~~~~~~~~~~
sorting.cpp:89:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (i < v.size()){
| ~~^~~~~~~~~~
# | 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... |