This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir-Rahmanir-Rahim
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
# define pii pair <int, int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 7;
const ll inf = 2e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll ans = 0;
pll p[maxn];
vector <ll> qwe[2][maxn];
void upd (ll x, ll id) {
if (x < 0) {
p[-x].ff = id;
}
else {
p[x].ss = id;
}
}
void f (vector <int>& v, ll x1, ll x2) {
ll dx = 0;
if (x1 <= x2) {
dx = 1;
}
else {
dx = -1;
}
while (x1 != x2) {
ans++;
swap(v[x1], v[x1 + dx]);
upd(v[x1], x1);
upd(v[x1 + dx], x1 + dx);
x1 += dx;
}
}
ll count (ll x, ll j) {
ll pos1 = 2 * j, pos2 = 2 * j + 1;
if (p[x].ff < p[x].ss) {
return abs(pos1 - p[x].ff) + abs(pos2 - p[x].ss);
}
else {
return abs(pos1 - p[x].ff) + abs(pos2 - p[x].ss) + 1;
}
}
long long count_swaps(vector<int> S){
ll n = sz(S);
set <ll> st, zxc;
for (ll i = 0; i < n + n; ++i) {
zxc.insert(abs(S[i]));
// upd(x, i);
if (S[i] < 0) {
qwe[0][-S[i]].pb(i);
}
else {
qwe[1][S[i]].pb(i);
}
}
ll sz = 0;
for (ll x : zxc) {
for (ll i = 0; i < sz(qwe[0][x]); ++i) {
++sz;
st.insert(sz);
ll pos1 = qwe[0][x][i], pos2 = qwe[1][x][i];
upd(-sz, pos1);
upd(sz, pos2);
S[pos1] = -sz, S[pos2] = sz;
}
}
for (ll j = 0; j < n; ++j) {
ll best = inf;
ll opt = -1;
for (ll x : st) {
ll dist = count(x, j);
if (dist < best) {
best = dist;
opt = x;
}
}
cout << opt << ' ' << best << nl;
st.erase(opt);
f(S, p[opt].ff, 2 * j);
f(S, p[opt].ss, 2 * j + 1);
}
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... |