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 "shoes.h"
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
const int maxn = 200100;
using ll = long long;
int v[maxn], n;
bool used[maxn];
void add(int i, int d) {
for (;i <= n;i += i & -i) v[i] += d;
}
int query(int i) {
int res = 0;
for (;i;i ^= i & -i) res += v[i];
return res;
}
vector<int> pos[2][maxn];
long long count_swaps(std::vector<int> s) {
ll res = 0;
n = s.size();
fill(used, used + n + 1, false);
fill(v, v + n + 1, 0);
for (int i = 0;i <= n;++i)
pos[0][i].clear(), pos[1][i].clear();
for (int i = n-1;i >= 0;--i)
pos[s[i] > 0][ abs(s[i]) ].pb(i);
for (int i = 0;i < n;++i) if (!used[i]) {
pos[s[i] > 0][ abs(s[i]) ].pop_back();
vector<int> &v = pos[s[i] < 0][ abs(s[i]) ];
res += (v.back()-i-1) - (query(v.back()-1) - query(i)) + (s[i] > 0);
used[ v.back() ] = true;
add( v.back(), 1);
v.pop_back();
}
return res;
}
# | 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... |