# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
65066 | Bodo171 | Sorting (IOI15_sorting) | C++14 | 582 ms | 19488 KiB |
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 <iostream>
const int nmax=200005;
using namespace std;
int i,n,idx,u;
int en[nmax],v[nmax],p[nmax],poz[nmax],a[3*nmax],b[3*nmax],A[3*nmax],B[3*nmax];
bool check(int val)
{
for(i=0;i<n;i++)
poz[v[i]]=i,p[i]=v[i];
for(i=0;i<val;i++)
swap(v[a[i]],v[b[i]]);
for(i=0;i<n;i++)
en[v[i]]=i;//en[i]-pozitia pe care ajunge elementul i;
idx=0;u=0;
for(i=0;i<val;i++)
{
swap(p[a[i]],p[b[i]]);
swap(poz[p[a[i]]],poz[p[b[i]]]);
while(idx<n&&en[idx]==idx) idx++;
if(idx<n)
{
en[v[idx]]=en[idx];//acum elementul care era pe pozitia noastra ajunge unde ar fi ajuns al noostru
v[en[idx]]=v[idx];//facem asta si la nivelul sirului
A[u]=poz[idx];B[u]=poz[v[idx]];
++u;
swap(poz[idx],poz[v[idx]]);
swap(p[poz[idx]],p[poz[v[idx]]]);
idx++;
}
}
while(idx<n&&en[idx]==idx) idx++;
return (idx==n);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int ans=M;
n=N;
for(i=0;i<M;i++)
a[i]=X[i],b[i]=Y[i];
for(int p=19;p>=0;p--)
{
for(i=0;i<N;i++)
v[i]=S[i];
if(ans-(1<<p)>=0&&check(ans-(1<<p)))
ans-=(1<<p);
}
for(i=0;i<N;i++)
v[i]=S[i];
int okceva=check(ans);
for(i=0;i<u;i++)
P[i]=A[i],Q[i]=B[i];
for(i=u;i<ans;i++)
P[i]=Q[i]=0;
return ans;
}
Compilation message (stderr)
# | 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... |