Submission #826853

#TimeUsernameProblemLanguageResultExecution timeMemory
826853vjudge1Arranging Shoes (IOI19_shoes)C++17
45 / 100
229 ms207732 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...