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>
using namespace std;
#define int long long int
const int N = 2e5+10;
int n, fenwick[N];
queue<int> shoes_l[N], shoes_r[N];
void upd(int pos, int val) {
for(int i = pos; i <= n; i+= i&-i) {
fenwick[i]+= val;
}
}
int get(int pos) {
int val = 0;
for(int i = pos; i > 0; i-= i&-i) {
val+= fenwick[i];
}
return val;
}
int count_swaps(vector<int32_t> s) {
n = s.size();
int ans = 0;
for(int i = 1; i <= n; i++) {
int temp = s[i-1];
if(temp < 0) {
temp *= -1;
if(shoes_r[temp].size()){
int pos = shoes_r[temp].front(); shoes_r[temp].pop();ans+= get(i) - get(pos-1);upd(pos,1);
}
else {
shoes_l[temp].push(i); upd(i,1);
}
}
else {
if(shoes_l[temp].size()){
int pos = shoes_l[temp].front(); shoes_l[temp].pop();ans+= get(i)-get(pos);upd(pos,1);
}
else {
shoes_r[temp].push(i); upd(i,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... |