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 ll long long
#define pb push_back
#define en '\n'
#define all(v) v.begin(),v.end()
const int N = 3e5 + 10;
int a[N],n;
deque <int> pos[N];
int t[N * 4];
void upd(int v,int tl,int tr,int pos) {
if(tl == tr) {
t[v] = 1;
return;
}
int tm = (tl + tr) / 2;
if(pos <= tm)upd(v + v,tl,tm,pos);
else upd(v + v + 1,tm + 1,tr,pos);
t[v] = t[v + v] + t[v + v + 1];
}
int get(int v,int tl,int tr,int l,int r) {
if(tl > r || tr < l)return 0;
if(tl >= l && tr <= r)return t[v];
int tm = (tl + tr) / 2;
return get(v + v,tl,tm,l,r) + get(v + v + 1,tm + 1,tr,l,r);
}
ll count_swaps(vector<int> S) {
n = S.size();
deque <int> q;
for(int i = 0;i < n;i++) {
a[i + 1] = S[i];
if(S[i] > 0)pos[S[i]].pb(i + 1);
else q.pb(i + 1);
}
n /= 2;
ll ans = 0;
for(int i = 1;i <= n;i++) {
int p = q.front();
q.pop_front();
int curpos = p + get(1,1,2 * n,p + 1,2 * n);
int need = 2 * i - 1;
ans += curpos - need;
upd(1,1,2 * n,p);
int val = -a[p];
p = pos[val].front();
pos[val].pop_front();
curpos = p + get(1,1,2 * n,p + 1,2 * n);
need++;
ans += curpos - need;
upd(1,1,2 * n,p);
}
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... |