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<iostream>
#include<vector>
using namespace std;
using ll = long long;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define dforn(i, n) for(int i=n-1; i>=0; --i)
#define PB push_back
const int MAXN = 100010;
bool del[2*MAXN];
int n, bit[2*MAXN];
vector<int> ubs[2*MAXN];
int sum(int r){
int ret=0;
for(; r>=0; r=(r&(r+1))-1) ret+=bit[r];
return ret;
}
void update(int r, int delta){
for(; r<2*MAXN; r=r|(r+1)) bit[r]+=delta;
}
long long count_swaps(vector<int> s) {
n = s.size()>>1;
ll ret=0;
dforn(i, 2*n) ubs[n+s[i]].PB(i);
forn(i, 2*n) if(!del[i]){
int assi = ubs[n-s[i]].back();
ubs[n+s[i]].pop_back();
ubs[n-s[i]].pop_back();
del[i]=del[assi]=true;
ret+=(assi-i-1-sum(assi-1)+sum(i) + (s[i]>0));
update(i, 1), update(assi, 1);
}
return ret;
}
# | 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... |