#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
using vi=vector<ll>;
using vb=vector<bool>;
#include "sorting.h"
int findSwapPairs(int n,int s[],int m,int x[],int y[],int P[],int Q[]){
int ok=m+1,ng=-1;
rep(i,m)P[i]=Q[i]=0;
while(ok-ng>1){
ll md=(ok+ng)/2;
vi v(n);
rep(i,n)v[i]=s[i];
rep(i,md)swap(v[x[i]],v[y[i]]);
vb used(n,false);
ll ans=n;
rep(i,n)if(!used[i]){
ans--;
ll w=i;
used[w]=true;
while(!used[v[w]]){
used[v[w]]=true;
w=v[w];
}
}
if(ans>md)ng=md;
else ok=md;
}
vi v(n);
rep(i,n)v[i]=s[i];
rep(i,ok)swap(v[x[i]],v[y[i]]);
vi target(n);rep(i,n)target[i]=i;
vi rev(n);
rep(i,n)rev[v[i]]=i;
set<ll> yet;
rep(i,n)if(v[i]!=target[i])yet.insert(v[i]);
for(int i=ok-1;i>=0;i--){
if(yet.size()){
ll w=rev[*yet.begin()];
if(v[w]==target[w]){
yet.erase(yet.begin());
i++;continue;
}
ll t=rev[target[w]];yet.erase(target[w]);
P[i]=t;Q[i]=w;
swap(target[t],target[w]);
}
swap(rev[v[x[i]]],rev[v[y[i]]]);
swap(target[x[i]],target[y[i]]);swap(v[x[i]],v[y[i]]);
}
return ok;
}/*
int main(){
int n;cin>>n;
int s[10];rep(i,n)cin>>s[i];
int m;cin>>m;
int x[10],y[10];
rep(i,m)cin>>x[i]>>y[i];
int p[10],q[10];
out(findSwapPairs(n,s,m,x,y,p,q));
rep(i,m)outp(P(p[i],q[i]));
rep(i,m){
swap(s[x[i]],s[y[i]]);
swap(s[p[i]],s[q[i]]);
}
rep(i,n)cout<<s[i]<<' ';cout<<endl;
}*/
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:43:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
43 | if(ans>md)ng=md;
| ^~
sorting.cpp:44:17: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
44 | else ok=md;
| ^~
sorting.cpp:62:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
62 | P[i]=t;Q[i]=w;
| ^
sorting.cpp:62:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
62 | P[i]=t;Q[i]=w;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
748 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
2 ms |
620 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
419 ms |
31648 KB |
Output is correct |
16 |
Correct |
436 ms |
32484 KB |
Output is correct |
17 |
Correct |
477 ms |
34076 KB |
Output is correct |
18 |
Correct |
80 ms |
22092 KB |
Output is correct |
19 |
Correct |
282 ms |
30196 KB |
Output is correct |
20 |
Correct |
301 ms |
31308 KB |
Output is correct |
21 |
Correct |
293 ms |
31436 KB |
Output is correct |
22 |
Correct |
398 ms |
29828 KB |
Output is correct |
23 |
Correct |
433 ms |
35560 KB |
Output is correct |
24 |
Correct |
476 ms |
34940 KB |
Output is correct |
25 |
Correct |
473 ms |
34448 KB |
Output is correct |
26 |
Correct |
306 ms |
30796 KB |
Output is correct |
27 |
Correct |
243 ms |
29240 KB |
Output is correct |
28 |
Correct |
453 ms |
35064 KB |
Output is correct |
29 |
Correct |
450 ms |
34184 KB |
Output is correct |
30 |
Correct |
190 ms |
28900 KB |
Output is correct |
31 |
Correct |
469 ms |
35276 KB |
Output is correct |
32 |
Correct |
445 ms |
34080 KB |
Output is correct |
33 |
Correct |
504 ms |
34940 KB |
Output is correct |
34 |
Correct |
478 ms |
34820 KB |
Output is correct |
35 |
Correct |
288 ms |
29968 KB |
Output is correct |
36 |
Correct |
70 ms |
23664 KB |
Output is correct |
37 |
Correct |
479 ms |
35404 KB |
Output is correct |
38 |
Correct |
472 ms |
34576 KB |
Output is correct |
39 |
Correct |
472 ms |
33924 KB |
Output is correct |