Submission #909397

#TimeUsernameProblemLanguageResultExecution timeMemory
909397GrayArranging Shoes (IOI19_shoes)C++17
100 / 100
384 ms150060 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define ln "\n"
const ll INF = 2e18;
const ll MOD = 1e9+7;
using namespace std;

struct Fenwick{
	vector<ll> t;
	ll n;
	Fenwick(ll sz){
		n = sz;
		t.resize(n+1, 0);
	}
	void update(ll i, ll ch){
		for (; i<=n; i+=(-i&i)) t[i]+=ch;
	}
	ll get(ll i){
		ll sum = 0;
		for (; i; i-=(-i&i)) sum+=t[i];
		return sum;
	}
};

long long count_swaps(vector<int> S){
	ll n = S.size()/2;
	Fenwick tr(2*n+1);
	for (ll i=0; i<2*n; i++){
		tr.update(i+1, 1);
	}
	unordered_map<ll, queue<ll>> pt;
	vector<ll> elp(2*n, -1);
	for (ll i=0; i<2*n; i++){
		if (pt[-S[i]].empty()) {
			pt[S[i]].push(i);
		}else{
			ll front = pt[-S[i]].front();
			pt[-S[i]].pop();
			elp[front]=i;
		}
	}
	ll ans = 0;
	for (ll i=0; i<2*n; i++){
		if (elp[i]==-1) continue;
		ll dif = tr.get(elp[i]+1)-tr.get(i+1)-1;
		if (S[i]>0){
			dif++;
		}
		ans+=dif;
		tr.update(elp[i]+1, -1);
		tr.update(i+1, -1);
	}
	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...