Submission #545336

#TimeUsernameProblemLanguageResultExecution timeMemory
545336JomnoiArranging Shoes (IOI19_shoes)C++17
10 / 100
87 ms134920 KiB
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;

const int MAX_N = 2e5 + 10;

queue <int> pos[MAX_N];

class FenwickTree {
private :
	int N;
	vector <int> fenwick;
public :
	FenwickTree() {}
	FenwickTree(int n) : N(n), fenwick(N + 1) {}

	void update(int idx, int val) {
		for(int x = idx; x > 0; x -= (x & -x)) {
			fenwick[x] += val;
		}
	}

	int query(int idx) {
		int res = 0;
		for(int x = idx; x <= N; x += (x & -x)) {
			res += fenwick[x];
		}
		return res;
	}
}fw;

long long count_swaps(vector<int> s) {
	int n = s.size() / 2;
	fw = FenwickTree(2 * n);
	for(int i = 0; i < 2 * n; i++) {
		int idx = s[i] + n;
		if(s[i] < 0) {
			idx = -s[i];
		}
		pos[idx].push(i + 1);
	}

	long long ans = 0;
	for(int i = 1; i <= n; i++) {
		while(!pos[i].empty()) {
			if(pos[i].front() < pos[i + n].front()) {
				ans += pos[i + n].front() - pos[i].front() - 1 + fw.query(pos[i].front());
				fw.update(pos[i + n].front(), 1);
				fw.update(pos[i].front(), -1);
			}
			else {
				ans += pos[i].front() - pos[i + n].front() + fw.query(pos[i + n].front());
				fw.update(pos[i].front(), 1);
				fw.update(pos[i + n].front(), -1);
			}
			pos[i].pop();
			pos[i + n].pop();
		}
	}
	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...