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;
typedef long long ll;
typedef pair <int, int> pii;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
inline bool isvowel(char ch){
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
inline bool isprime(int n){
if(n < 2 || (n%2 == 0 && n != 2)) return false;
for(int i = 3; i*i <= n; i++)
if(n%i == 0) return false;
return true;
}
const int NN = 6e5+7;
int S[NN], X[NN], Y[NN], tmpS[NN], tmpos[NN];
int n;
vector <pii> res1;
bool check(int poss){
int j;
vector <pii> sws;
for(j = 0; j < n; j++)
tmpS[j] = S[j];
for(j = 0; j < poss; j++)
swap(tmpS[X[j]], tmpS[Y[j]]);
for(j = 0; j < n; j++)
tmpos[tmpS[j]] = j;
for(j = 0; j < n; j++){
if(tmpos[j] == j) continue;
sws.pb(mp(tmpS[j], tmpS[tmpos[j]]));
swap(tmpS[j], tmpS[tmpos[j]]);
swap(tmpos[tmpS[j]], tmpos[tmpS[tmpos[j]]]);
}
if(sz(sws) <= poss){
for(j = 0; j < n; j++){
tmpS[j] = S[j];
tmpos[S[j]] = j;
}
for(j = 0; j < sz(sws); j++){
swap(tmpS[X[j]], tmpS[Y[j]]);
swap(tmpos[tmpS[X[j]]], tmpos[tmpS[Y[j]]]);
res1.pb(mp(tmpos[sws[j].first], tmpos[sws[j].second]));
swap(tmpS[tmpos[sws[j].first]], tmpS[tmpos[sws[j].second]]);
swap(tmpos[tmpS[tmpos[sws[j].first]]], tmpos[tmpS[tmpos[sws[j].second]]]);
}
while(sz(res1) < poss){
res1.pb(mp(0, 0));
}
return 1;
}
return 0;
}
int findSwapPairs(int N, int s[], int M, int x[], int y[], int P[], int Q[]) {
int i;
n = N;
for(i = 0; i < N; i++){
S[i] = s[i];
}
for(i = 0; i < M; i++){
X[i] = x[i];
Y[i] = y[i];
}
int l = 0, r = M;
int mn = 2e9;
while(l <= r){
int mid = (l+r) >> 1;
res1.clear();
if(check(mid)){
mn = min(mid, mn);
r = mid-1;
}
else{
l = mid+1;
}
}
for(i = 0; i < mn; i++){
P[i] = res1[i].first;
Q[i] = res1[i].second;
}
return mn;
}
Compilation message (stderr)
sorting.cpp: In function 'bool isvowel(char)':
sorting.cpp:23:14: warning: conversion from 'int' to 'char' may change value [-Wconversion]
23 | ch = tolower(ch);
| ~~~~~~~^~~~
# | 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... |