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()
#define pii pair <int,int>
#define f first
#define s second
#define mp make_pair
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);
}
bool cmp(pii x,pii y) {
return x.f + x.s + (x.f > x.s) < y.f + y.s + (y.f > y.s);
}
vector <pii> g;
void rebuild(int L) {
g.clear();
vector <int> q;
for(int i = L;i <= 2 * n;i++) {
if(a[i] > 0)pos[a[i]].pb(i);
else q.pb(i);
}
for(auto to : q) {
int val = -a[to];
int cor = pos[val].front();
pos[val].pop_front();
g.pb(mp(to,cor));
//cout << to << " " << cor << en;
}
}
ll count_swaps(vector<int> S) {
n = S.size();
vector <int> q;
for(int i = 0;i < n;i++) {
a[i + 1] = S[i];
}
n /= 2;
rebuild(1);
sort(all(g),cmp);
ll ans1 = 0;
for(int i = 1;i <= n;i++) {
int L = g[i - 1].f,R = g[i - 1].s;
int pL = L + get(1,1,2 * n,L,2 * n);
int pR = R + get(1,1,2 * n,R,2 * n);
ans1 += pL + pR - (4 * i - 1);
if(pL > pR)ans1++;
upd(1,1,2 * n,L);
upd(1,1,2 * n,R);
}
return ans1;
}
| # | 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... |