#include "sorting.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)
ll n,s[600001],m,x[600001],y[600001],p[600001],q[600001];
vector<vector<ll>> temp;
int check(ll k){
vector<ll> sus;
unordered_map<ll, ll> realsus;
temp.clear();
FOR(i,0,n){
sus.push_back(s[i]);
realsus[s[i]] = i;
}
FOR(i,0,k){
swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
swap(sus[x[i]], sus[y[i]]);
}
cout << endl;
ll ans = 0;
FOR(i,0,n){
if (realsus[i]==i) continue;
ans++;
ll amogus = sus[i];
temp.push_back({i, sus[i]});
swap(sus[realsus[i]], sus[i]);
swap(realsus[i], realsus[amogus]);
}
if (ans<k){
FOR(i,0,k-ans){
temp.push_back({0,0});
}
}
if (ans <= k) return 1;
else return 0;
}
void genans(ll k){
vector<ll> sus;
unordered_map<ll, ll> realsus;
FOR(i,0,n){
sus.push_back(s[i]);
realsus[s[i]] = i;
}
FOR(i,0,k){
swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
swap(sus[x[i]], sus[y[i]]);
p[i] = realsus[temp[i][0]];
q[i] = realsus[temp[i][1]];
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
FOR(i,0,n){
s[i] = S[i];
}
m = M;
FOR(i,0,m){
x[i] = X[i];
y[i] = Y[i];
}
ll lo = 0;
ll hi = m+1;
while (lo<hi){
int mid = lo + (hi - lo) / 2;
if (check(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
check(lo);
genans(lo);
FOR(i,0,lo){
P[i] = p[i];
Q[i] = q[i];
}
return lo;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:85:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
85 | int mid = lo + (hi - lo) / 2;
| ~~~^~~~~~~~~~~~~~~
sorting.cpp:95:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
95 | P[i] = p[i];
| ~~~^
sorting.cpp:96:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
96 | Q[i] = q[i];
| ~~~^
sorting.cpp:98:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
98 | return lo;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |