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 push_back
// #define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;
struct fenwick{
int n;
vector<int> fen;
fenwick(int n) : n(n), fen(n){}
void upd(int pos, int val){
for(; pos < n; pos |= pos + 1)
fen[pos] += val;
}
int index(int ind){
int ret = 0;
for(; ind >= 0; ind = (ind & (ind + 1)) - 1)
ret += fen[ind];
return ret;
}
};
ll count_swaps(vector<int> v){
int n = v.size() / 2;
vector<int> negative;
vector<vector<int>> occurence(n + 1);
for(int i = 2 * n - 1; i >= 0; i--){
if(v[i] < 0) negative.pb(i);
else occurence[v[i]].pb(i);
}
fenwick fen(2 * n);
ll ans = 0;
for(int i = 0; i < 2 * n; i++)
fen.upd(i, 1);
for(int i = 0; i < n; i++){
int neg = negative.back(); negative.pop_back();
int val = v[neg];
fen.upd(neg, -1);
neg = fen.index(neg);
ans += neg;
int pos = occurence[-val].back();
occurence[-val].pop_back();
fen.upd(pos, -1);
pos = fen.index(pos);
ans += pos;
}
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... |