# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
397091 | Andyvanh1 | Sorting (IOI15_sorting) | C++14 | 209 ms | 21256 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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
using namespace std;
#define pb push_back
#define INF INT32_MAX
#define vt vector
typedef vt<int> vi;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tiii;
typedef long long ll;
#define MOD 1000000007
bool works(int n, int s[], int m, int x[],int y[], int val){
for(int i = 0; i < val; i++){
swap(s[x[i]],s[y[i]]);
}
vt<bool> visited(n);
for(int i = 0; i < n; i++){
visited[i] = false;
}
int ans = 0;
for(int i = 0; i < n; i++){
if(!visited[i]){
int j = s[i];
visited[i] = true;
while(j!=i){
ans++;
visited[j] = true;
j = s[j];
}
}
}
for(int i = val-1; i >= 0; i--){
swap(s[x[i]],s[y[i]]);
}
return val>=ans;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]){
int l = 0, r= m-1;
while(l!=r){
int mid = (l+r)>>1;
if(works(n,s,m,x,y,mid)){
r = mid;
}else{
l = mid+1;
}
}
int op[r];
int oq[r];
for(int i = 0; i < r; i++){
swap(s[x[i]],s[y[i]]);
}
int index = 0;
vt<bool> visited(n);
for(int i = 0; i < n; i++){
visited[i] = false;
}
for(int i = 0; i < n; i++){
if(!visited[i]){
int j = i;
visited[i] = true;
vi curop;
vi curoq;
while(s[j]!=i){
visited[s[j]] = true;
curop.pb(j);
j = s[j];
curoq.pb(j);
}
for(int i = index; i < index+curop.size(); i++){
op[i] = curop[index+curop.size()-1-i];
oq[i] = curoq[index+curop.size()-1-i];
}
index+=curop.size();
}
}
for(int i = index; i < r; i++){
op[i]=1;
oq[i]=1;
}
for(int i = r-1; i >= 0; i--){
swap(s[x[i]],s[y[i]]);
}
int revs[n];
for(int i = 0; i < n; i++){
revs[s[i]] = i;
}
for(int i = 0; i < r; i++){
swap(revs[s[x[i]]],revs[s[y[i]]]);
swap(s[x[i]],s[y[i]]);
p[i] = revs[op[i]];
q[i] = revs[oq[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... |