Submission #587099

#TimeUsernameProblemLanguageResultExecution timeMemory
587099ZaniteArranging Shoes (IOI19_shoes)C++17
50 / 100
1094 ms277060 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

using ll	= long long;
using pii	= pair<int, int>;
using pll	= pair<ll, ll>;

#define fi	first
#define se	second

ll count_swaps(vector<int> s) {
	int n = s.size();

	vector<int> rel(n);
	vector<deque<int>> occ(n, deque<int>(0));
	vector<pii> leftShoes;
	vector<pair<int, pii>> ordered;

	for (int i = 0; i < n; i++) {
		if (s[i] < 0) leftShoes.push_back({i, -s[i]});
		else occ[s[i]].push_back(i);
	}

	vector<deque<int>> tocc = occ;

	for (auto &[idx, typ] : leftShoes) {
		int right = tocc[typ].front();
		tocc[typ].pop_front();

		int d = right + idx - (int)(right < idx);
		ordered.push_back({d, {idx, typ}});
	}
	sort(ordered.begin(), ordered.end());

	int placed = 0;
	for (auto &[d, p] : ordered) {
		int idx, typ;
		tie(idx, typ) = p;

		rel[idx] = placed++;
		rel[occ[typ].front()] = placed++;
		occ[typ].pop_front();
	}

	ll ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i; j > 0; j--) {
			if (rel[j] < rel[j-1]) {
				swap(rel[j], rel[j-1]);
				ans++;
			}
		}
	}

	for (int i = 1; i < n; i++) {
		assert(rel[i] > rel[i-1]);
	}

	return ans;
}

// Grader

#ifdef Zanite

int main() {
	int n;
	assert(1 == scanf("%d", &n));
	vector<int> S(2 * n);
	for (int i = 0; i < 2 * n; i++)
		assert(1 == scanf("%d", &S[i]));
	fclose(stdin);

	long long result = count_swaps(S);

	printf("%lld\n", result);
	fclose(stdout);
	return 0;
}

#endif
#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...