Submission #769681

#TimeUsernameProblemLanguageResultExecution timeMemory
769681SanguineChameleonArranging Shoes (IOI19_shoes)C++17
100 / 100
47 ms17400 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 20;
int order[maxn * 2];
int sizes[maxn * 2];
int match[maxn * 2];
bool flag[maxn * 2];
int bit[maxn * 2];
vector<int> shoes[maxn][2];
int n;

void update(int pos) {
	for (int i = pos; i <= n * 2; i += i & (-i)) {
		bit[i]++;
	}
}

int get(int pos) {
	int res = 0;
	for (int i = pos; i > 0; i -= i & (-i)) {
		res += bit[i];
	}
	return res;
}

long long count_swaps(vector<int> _sizes) {
	n = (int)_sizes.size() / 2;
	for (int i = 1; i <= n * 2; i++) {
		sizes[i] = _sizes[i - 1];
		if (sizes[i] < 0) {
			shoes[-sizes[i]][0].push_back(i);
		}
		else {
			shoes[sizes[i]][1].push_back(i);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < (int)shoes[i][0].size(); j++) {
			match[shoes[i][0][j]] = shoes[i][1][j];
			match[shoes[i][1][j]] = shoes[i][0][j];
		}
	}
	int cnt = 0;
	for (int i = 1; i <= n * 2; i++) {
		if (!flag[i]) {
			order[i] = cnt + (sizes[i] < 0 ? 1 : 2);
			order[match[i]] = cnt + (sizes[match[i]] < 0 ? 1 : 2);
			flag[i] = true;
			flag[match[i]] = true;
			cnt += 2;
		}
	}
	long long res = 0;
	for (int i = n * 2; i >= 1; i--) {
		res += get(order[i]);
		update(order[i]);
	}
	return res;
}
#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...