# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
794934 | Username4132 | Sorting (IOI15_sorting) | C++14 | 1 ms | 340 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>
#include<vector>
#include<algorithm>
using namespace std;
using pii = pair<int, int>;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define forsn(i, s, n) for(int i=s; i<(int)n; ++i)
#define PB push_back
#define F first
#define S second
int n, cur;
vector<pii> gen(vector<int>& p){
vector<int> q(n);
forn(i, n) q[i]=p[i];
vector<pii> ret;
vector<bool> vis(n, false);
forn(i, n) if(!vis[i]){
vis[i]=true;
int x=p[i];
while(x!=i){
vis[x]=true;
ret.PB({q[i], q[x]});
swap(q[i], q[x]);
x=p[x];
}
}
return ret;
}
void mov(vector<int>& per, int* X, int* Y, int pos){
while(cur<pos){
swap(per[X[cur]], per[Y[cur]]);
++cur;
}
while(cur>pos){
--cur;
swap(per[X[cur]], per[Y[cur]]);
}
}
int findSwapPairs(int N, int seq[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
vector<int> per(n);
forn(i, n) per[i]=seq[i];
int lo=-1, hi=n;
while(hi-lo>1){
int mid = (hi+lo)/2;
mov(per, X, Y, mid);
vector<pii> res = gen(per);
if(res.size()>mid) lo=mid;
else hi=mid;
}
vector<pii> res = gen(per);
int ans = res.size();
vector<int> inv(n);
forn(i, n) inv[seq[i]]=i;
forn(i, ans){
int a = res[i].F, b = res[i].S;
swap(inv[seq[X[i]]], inv[seq[Y[i]]]);
swap(seq[X[i]], seq[Y[i]]);
P[i]=inv[a], Q[i]=inv[b];
swap(seq[P[i]], seq[Q[i]]);
swap(inv[a], inv[b]);
}
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... |