# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
791253 | TimDee | Sorting (IOI15_sorting) | C++17 | 98 ms | 21644 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 forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
int findSwapPairs(int n, int _p[], int m, int x[], int y[], int P[], int Q[]) {
int z=1;
forn(i,n) if (_p[i]!=i) z=0;
if (z) return 0;
int l=1, r=n;
while (l<r) {
int m=(l+r)>>1;
vector<int> p(n);
forn(i,n) p[i]=_p[i];
forn(i,m) {
swap(p[x[i]],p[y[i]]);
}
vector<int> pos(n);
forn(i,n) pos[p[i]]=i;
int z=0;
forn(i,n) {
if (p[i]!=i) {
z++;
pos[p[i]]=pos[i];
swap(p[pos[i]],p[i]);
}
}
if (z<=m) r=m;
else l=m+1;
}
for (int k=r; k<=m; ++k) {
vector<int> p(n);
forn(i,n) p[i]=_p[i];
auto a=p;
forn(i,k) {
swap(p[x[i]],p[y[i]]);
}
vector<int> pos(n);
forn(i,n) pos[p[i]]=i;
vector<pi> v;
forn(i,n) {
if (p[i]!=i) {
v.pb({i,p[i]});
pos[p[i]]=pos[i];
swap(p[pos[i]],p[i]);
}
}
if (v.size()<=k) {
while (v.size()<k) v.pb({0,0});
forn(i,n) pos[a[i]]=i;
vector<pi> e;
forn(i,k) {
swap(pos[a[x[i]]],pos[a[y[i]]]);
swap(a[x[i]],a[y[i]]);
e.pb({pos[v[i].f],pos[v[i].s]});
swap(pos[v[i].f],pos[v[i].s]);
swap(a[pos[v[i].f]],a[pos[v[i].s]]);
}
forn(i,k) P[i]=e[i].f, Q[i]=e[i].s;
return k;
}
}
return 0;
}
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... |