이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define trav(a, x) for (auto &a : x)
#define pb push_back
#define el "\n";
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
ll count_swaps(vector<int> S) {
Tree<ll> t;
ll n = sz(S) / 2; // number of pairs
vi a = S;
vector<bool> done(2 * n);
vector<set<ll>> pos(2 * n + 1);
rep(i, 0, 2*n) {
pos[a[i] + n].insert(i);
}
ll ans = 0;
ll ind = 0;
while(ind < 2 * n) {
if (done[ind]) {
ind++;
continue;
}
ll cur = a[ind];
auto buddy = pos[-cur + n].upper_bound(ind);
ll bigger_than_buddy = t.order_of_key(-(*buddy));
ll bigger_than_ind = t.order_of_key(-ind);
ans += (*buddy) - ind - (cur < 0 ? 1 : 0) + bigger_than_buddy - bigger_than_ind;
t.insert(-(*buddy));
pos[-cur + n].erase(*buddy);
done[ind] = 1;
done[*buddy] = 1;
ind++;
}
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... |