# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
790083 | PoonYaPat | Sorting (IOI15_sorting) | C++14 | 2 ms | 468 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;
typedef pair<int,int> pii;
int n,m,b[200005],x[200005],y[200005],p[200005],q[200005];
vector<pii> ans;
int a[200005],pos[200005],val[200005];
bool check(int h, bool mode) {
for (int i=0; i<n; ++i) a[i]=b[i], val[i]=i;
for (int i=0; i<=h; ++i) swap(a[x[i]],a[y[i]]);
vector<pii> v;
for (int i=0; i<n; ++i) pos[a[i]]=i;
for (int i=0; i<n; ++i) {
if (i!=pos[i]) {
v.push_back(pii(i,pos[i]));
int bef=a[i];
a[pos[i]]=a[i];
a[i]=i;
pos[bef]=pos[i];
pos[i]=i;
}
}
if (mode) {
for (int i=0; i<=h; ++i) {
swap(val[x[i]],val[y[i]]);
if (v.size()) {
ans.push_back(pii(val[v.back().first],val[v.back().second]));
v.pop_back();
} else ans.push_back(pii(0,0));
}
}
if (v.size()<=h+1) return true;
else return false;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N; m=M;
for (int i=0; i<n; ++i) b[i]=S[i], x[i]=X[i], y[i]=Y[i];
int l=0, r=m-1;
while (l<r) {
int mid=(l+r)/2;
if (check(mid,0)) r=mid;
else l=mid+1;
}
check(l,1);
for (int i=0; i<ans.size(); ++i) P[i]=ans[i].first, Q[i]=ans[i].second;
return ans.size();
}
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... |