Submission #959187

#TimeUsernameProblemLanguageResultExecution timeMemory
959187maxFedorchukSorting (IOI15_sorting)C++17
100 / 100
184 ms23236 KiB
#include <bits/stdc++.h> using namespace std; int n,m,*x,*y,*s,*p,*q; const long long MX=2e5+10; int a[MX],ai[MX]; int b[MX],bi[MX]; bool f(int k) { for(int i=0;i<n;i++) { a[i]=s[i]; b[i]=s[i]; } for(int i=0;i<k;i++) { swap(a[x[i]],a[y[i]]); } for(int i=0;i<n;i++) { ai[a[i]]=i; bi[b[i]]=i; } int uk=0; for(int i=0;i<k;i++) { swap(bi[b[x[i]]],bi[b[y[i]]]); swap(b[x[i]],b[y[i]]); while(uk<n && a[uk]==uk) { uk++; } if(uk==n) { p[i]=0; q[i]=0; continue; } p[i]=bi[uk]; q[i]=bi[a[uk]]; swap(b[p[i]],b[q[i]]); swap(bi[uk],bi[a[uk]]); ai[a[uk]]=ai[uk]; swap(a[uk],a[ai[uk]]); ai[uk]=uk; } bool o=1; for(int i=0;i<n;i++) { o&=(a[i]==i); } return o; } 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;p=P;q=Q; int l=-1,r=M; while(l+1<r) { int mid=(l+r)/2; if(f(mid)) { r=mid; } else { l=mid; } } f(r); return r; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...