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 <bits/stdc++.h>
#include <iostream>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll ans = 0;
int n;
vi shoes;
int f(int x, int m) {
return (x > 0) ? x : m - 1 + x;
}
void rem() {
int pairs = n/2;
rep(pair, 0, pairs) {
vi dist(pairs, 0);
rep(i,0,n) dist[shoes[i]] += i;
int bd = 1e9, p;
rep(i, 0, pairs) {
if (dist[i] < bd) {bd = dist[i]; p = i;}
}
ans += bd - 1;
rep(i, 0, n-1) if (shoes[i] == p) swap(shoes[i], shoes[i + 1]);
rep(i, 0, n-1) if (shoes[i] == p) swap(shoes[i], shoes[i + 1]);
}
}
ll count_swaps(vi S)
{
n = S.size(); shoes.resize(n); rep(i,0,n) shoes[i] = S[i];
rep(i,0,n) shoes[i] = -shoes[i];
vvi exists(2*n + 5);
rep(i,0,n) exists[f(shoes[i], 2*n+5)].pb(i);
int real = 0;
rep(i,1,n+1) {
rep(j, 0, (int)exists[i].size()) {
int a = exists[i][j], b = exists[f(-i, 2*n+5)][j];
if (a > b) ans++;
shoes[a] = shoes[b] = real;
real++;
}
}
rem();
return ans;
}
# | 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... |