# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
96892 | figter001 | Sorting (IOI15_sorting) | C++14 | 3 ms | 512 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 long long ll;
const int maxn = 2e5+50;
vector<int> a;
vector<pair<int,int>> s;
int idx[maxn];
bool can(int md){
vector<int> b = a;
for(int i=0;i<md;i++)
swap(b[s[i].first],b[s[i].second]);
int cnt = 0;
for(int i=0;i<a.size();i++){
while(b[i] != i){
cnt ++;
swap(b[i],b[b[i]]);
}
}
return cnt <= md;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
for(int i=0;i<N;i++)a.push_back(S[i]);
for(int i=0;i<M;i++)s.push_back({X[i],Y[i]});
int md,lo=0,hi = M,ans;
while(lo <= hi){
md = (lo + hi)/2;
if(can(md)){
hi = md-1;
ans = md;
}else lo = md+1;
}
vector<int> b = a;
for(int i=0;i<ans;i++)swap(a[s[i].first],a[s[i].second]);
vector<pair<int,int>> c;
for(int i=0;i<a.size();i++){
while(a[i] != i){
c.push_back({a[a[i]],a[i]});
swap(a[i],a[a[i]]);
}
}
a = b;
while(c.size() < md)c.push_back({0,0});
for(int i=0;i<N;i++)idx[a[i]] = i;
for(int i=0;i<ans;i++){
int l = s[i].first;
int r = s[i].second;
swap(idx[a[l]],idx[a[r]]);
swap(a[l],a[r]);
l = c[i].first;
r = c[i].second;
P[i] = idx[l];
Q[i] = idx[r];
swap(a[P[i]],a[Q[i]]);
swap(idx[l],idx[r]);
}
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... |