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;
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& ans) {
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;
}
}
long long count_swaps(vector<int> S){
ll ans = 0;
ll n = sz(S) / 2;
set <ll> zxc;
for (ll i = 0; i < n + n; ++i) {
zxc.insert(abs(S[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;
ll pos1 = qwe[0][x][i], pos2 = qwe[1][x][i];
S[pos1] = -sz, S[pos2] = sz;
}
}
for (ll i = 0; i < n + n; ++i) {
upd(S[i], i);
}
for (ll j = 0; j < n + n; j += 2) {
//j, j + 1
ll x = abs(S[j]);
// p[x].ff -> j, p[x].ss -> j + 1
f(S, p[x].ff, j, ans); f(S, p[x].ss, j + 1, ans);
}
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... |