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 <bits/stdc++.h>
#include "shoes.h"
#define all(x) x.begin(),x.end()
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 200100;
vector<int> vc[N];
ll ans = 0;
int fen[N];
bool mrk[N];
int sum(int x){
int res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1)
res += fen[x];
return res;
}
void upd(int x, int vl){
for (; x < N; x = (x | (x + 1)))
fen[x] += vl;
}
long long count_swaps(std::vector<int> s) {
int n = sz(s) / 2;
for (int i = sz(s) - 1; i >= 0; i--)
vc[s[i] + n].PB(i);
for (int i = 0; i < sz(s); i++){
if (mrk[i]) continue;
if (s[i] > 0)
ans++;
vc[s[i] + n].pop_back();
int real_i = i + sum(i);
int j = vc[-s[i] + n].back();
vc[-s[i] + n].pop_back();
mrk[j] = 1;
int real_j = sum(j) + j;
ans += real_j - real_i - 1;
upd(i, 1);
upd(j, -1);
}
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... |