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>
using namespace std;
#define bit(s, i) (s & (1 << i))
#define fi first
#define se second
#define pb push_back
#define endl '\n'
const int N = 2e5 + 5;
int Bit[N], cur[N];
queue<int> pos[N][2];
void update(int i){
for(; i < N; i += (i & -i))Bit[i]++;
}
int get(int i){
int res = 0;
for(; i > 0; i -= (i & -i)) res += Bit[i];
return res;
}
long long count_swaps(vector<int> a){
int n = a.size() / 2;
vector<int> solve;
for(int i = 0; i < 2 * n; i++){
int x = a[i];
if(x < 0){
if(cur[abs(x)] <= 0)solve.pb(abs(x));
pos[abs(x)][0].push(i+1);
cur[abs(x)]--;
} else{
if(cur[abs(x)] >= 0)solve.pb(abs(x));
pos[abs(x)][1].push(i+1);
cur[abs(x)]++;
}
}
long long res = 0;
int cnt = 0;
for(int x : solve){
int le = pos[x][0].front(), ri = pos[x][1].front();
pos[x][0].pop(), pos[x][1].pop();
if(le > ri){
res++;
swap(le,ri);
}
res += le + ri;
res -= 2 * (cnt + 2) - 1;
res -= get(le) - cnt;
update(le), cnt++;
res -= get(ri) - cnt;
update(ri), cnt++;
}
return res;
}
# | 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... |