# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
133644 | Mahdi_Jfri | Sorting (IOI15_sorting) | C++14 | 29 ms | 1272 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<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 20;
const int maxm = 3 * maxn;
int pos[maxn] , src[maxn] , a[maxn];
int p[maxm] , q[maxm] , x[maxm] , y[maxm] , n;
bool check(int m)
{
memcpy(a , src , sizeof src);
if(!m)
{
bool f = 1;
for(int i = 0; i < n; i++)
f &= a[i] == i;
return f;
}
swap(a[x[0]] , a[y[0]]);
for(int i = 0; i < n; i++)
pos[i] = i;
for(int i = m - 1; i > 0; i--)
swap(pos[x[i]] , pos[y[i]]);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
if(pos[j] != a[j])
{
int ind = -1;
for(int k = 0; k < n; k++)
if(pos[k] == a[j])
{
while(ind >= 0);
ind = k;
}
while(ind < 0);
swap(a[ind] , a[j]);
p[i] = ind , q[i] = j;
break;
}
if(i + 1 < m)
{
swap(a[x[i + 1]] , a[y[i + 1]]);
swap(pos[x[i + 1]] , pos[y[i + 1]]);
}
}
bool f = 1;
for(int i = 0; i < n; i++)
f &= a[i] == i;
return f;
}
int findSwapPairs(int N, int A[], int m, int X[], int Y[], int P[], int Q[])
{
n = N;
for(int i = 0; i < n; i++)
src[i] = A[i];
for(int i = 0; i < m; i++)
x[i] = X[i] , y[i] = Y[i];
int l = 0 , r = m;
if(check(l))
return l;
while(r - l > 1)
{
int m = (l + r) / 2;
if(check(m))
r = m;
else
l = m;
}
check(r);
for(int i = 0; i < r; i++)
P[i] = p[i] , Q[i] = q[i];
return r;
}
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... |