이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include "bits/stdc++.h"
using namespace std;
#define ssize(x) (int)size(x)
template <class A, class B>
auto &operator<<(ostream &o, pair<A, B> p)
{
return o << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &o, T x) -> decltype(x.end(), o)
{
o << '{';
int i = 0;
for (auto e : x)
o << (", ") + 2 * !i++ << e;
return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) { ((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) \
{ \
}
#endif
long long count_swaps(vector<int> s)
{
int n = ssize(s);
vector<vector<deque<int>>> pos(2, vector<deque<int>>(n + 1));
vector<int> pary(n);
vector<int> order;
for (int i = 0; i < n; i++) {
int rozm = abs(s[i]), sign = s[i] > 0;
if (ssize(pos[sign ^ 1][rozm])) {
int kto = pos[sign ^ 1][rozm].front();
pos[sign ^ 1][rozm].pop_front();
pary[kto] = i;
}
else {
pos[sign][rozm].push_back(i);
order.push_back(i);
}
}
debug(pary, order);
vector<int> fen(n + 1);
auto pisz = [&](int x) {
for (x++; x; x -= x & -x)
fen[x]++;
};
auto ile = [&](int x) {
int ans = 0;
for (x++; x <= n; x += x & -x)
ans += fen[x];
return ans;
};
long long ans = 0;
for (int x : order) {
int lo = x + ile(x);
int hi = pary[x] + ile(pary[x]);
ans += hi - lo - 1 + (s[x] > 0);
debug(ans);
pisz(pary[x]);
}
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... |