Submission #1361271

#TimeUsernameProblemLanguageResultExecution timeMemory
1361271TraianDanciuArranging Shoes (IOI19_shoes)C++20
100 / 100
31 ms14452 KiB
#include "shoes.h"

using namespace std;

const int MAXN = 200000;

vector<int> poz[MAXN + 1][2];
int match[MAXN + 1];

struct FenwickTree {
	int v[MAXN + 1], n;

	void init(int n) {
		this->n = n;
		for(int i = 1; i <= n; i++) {
			v[i] = (i & -i);
		}
	}

	void update(int poz) {
		while(poz <= n) {
			v[poz]--;
			poz += poz & -poz;
		}
	}

	int query(int poz) {
		int answer = 0;
		while(poz > 0) {
			answer += v[poz];
			poz &= poz - 1;
		}
		return answer;
	}
} aib;

long long count_swaps(vector<int> s) {
	int n = s.size();
	for(int i = 0; i < n; i++) {
		if(s[i] < 0) {
			poz[-s[i]][0].push_back(i + 1);
		} else {
			poz[s[i]][1].push_back(i + 1);
		}
	}

	for(int i = 1; i <= n / 2; i++) {
		for(int j = 0; j < (int)poz[i][0].size(); j++) {
			match[poz[i][0][j]] = poz[i][1][j];
			match[poz[i][1][j]] = poz[i][0][j];
		}
	}

	aib.init(n);
	long long answer = 0;
	for(int i = 1; i <= n; i++) {
		if(i < match[i]) {
			int cate = aib.query(match[i] - 1) - aib.query(i);
			if(s[i - 1] < 0) {
				answer += cate;
			} else {
				answer += cate + 1;
			}
			aib.update(match[i]);
		}
	}
	return answer;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...