Submission #377013

#TimeUsernameProblemLanguageResultExecution timeMemory
377013sliviuArranging Shoes (IOI19_shoes)C++14
100 / 100
335 ms274156 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll count_swaps(vector<int> v) {
	int n = v.size();
	ll ans = 0;
	vector<int> bit(n + 1);
	vector<queue<int>> Q(2 * n + 1);
	for (int i = 0; i < n; ++i)
		Q[n + v[i]].emplace(i + 1);
	auto update = [&](int pos, int val) {
		while (pos <= n) {
			bit[pos] += val;
			pos += pos & -pos;
		}
	};
	auto query = [&](int pos) {
		int ans = 0;
		while (pos) {
			ans += bit[pos];
			pos -= pos & -pos;
		}
		return ans;
	};
	for (int i = 1, j = 1; i <= n; ++i) {
		int cur = v[i - 1], cmp = -cur;
		if (!cur)
			continue;
		int top = Q[n + cmp].front();
		Q[n + cmp].pop(), Q[n + cur].pop();
		ans += (cur > 0) + top - j - 1 + query(top);
		update(i + 1, 1);
		update(top + 1, -1);
		v[top - 1] = 0, j += 2;
	}
	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...