Submission #419100

#TimeUsernameProblemLanguageResultExecution timeMemory
419100FlippenFazArranging Shoes (IOI19_shoes)C++14
45 / 100
813 ms163616 KiB
#include <bits/stdc++.h>
#include "shoes.h"

using namespace std;
typedef long long ll;

#define SIZE 524288

map<ll, queue<int>> shoesList;
vector<ll> offset;
vector<ll> decSet;

void incOff(int pos) {
	pos+=SIZE;
	while (pos > 0) {
		offset[pos]++;
		pos/=2;	
	}
}

void decOff(int pos) {
	pos+=SIZE;
	while (pos > 0) {
		decSet[pos]++;
		offset[pos]--;
		pos/=2;	
	}
}

ll getDec(int pos) {
	ll sum = 0;
	int left = SIZE;
	int right = pos + SIZE;
	while (left <= right) {
		if (left%2 == 1) {sum+=decSet[left++];}
		if (right%2 == 0) {sum+=decSet[right--];}
		left/=2;
		right/=2;
	}
	return sum;
}

ll getOff(int pos) {

	if(pos == -1) {
		return 0;
	}

	ll sum = 0;
	int left = SIZE;
	int right = pos + SIZE;
	while (left <= right) {
		if (left%2 == 1) {sum+=offset[left++];}
		if (right%2 == 0) {sum+=offset[right--];}
		left/=2;
		right/=2;
	}
	return sum;
}

ll count_swaps(vector<int> s) {
	ll sum = 0;

	offset.resize(SIZE*2);
	decSet.resize(SIZE*2);

	//cerr << "OFFSET: " << offset[0] << " " << offset[100] << endl << endl;

	for (ll i = 0; i < s.size(); i++) {
		ll pos = i;

		//cerr << endl;
		//cerr << "I: " << i << " SHOE: " << s[i] << endl;
		//cerr << "SIZE: " << shoesList[-s[i]].size() << "POS OF INVERSE:" << shoesList[-s[i]].front() << endl;
		//cerr << "CUROFFSET 0: " << offset[SIZE] << "TOTAL: " << offset[1] << endl;

		if (shoesList[-s[i]].size() == 0) {
			shoesList[s[i]].push(pos);
		} else {
			ll prev = -1;
			ll curPos = shoesList[-s[i]].front();
			//cerr << "GET OFF CUR: " << getOff(curPos) << "GET OFF PREV: " << getOff(prev) << endl;
			if ((getOff(curPos) - getOff(prev)) > 0) {
				ll temp = curPos;
				curPos = curPos + getOff(curPos) - getOff(prev);
				prev = temp;
				//cerr << endl;
				//cerr << "CURPOS: " << curPos << endl;
				while ( (getOff(curPos) - getOff(prev) + getDec(curPos) - getDec(prev)) > 0) {
					ll temp = curPos;
					curPos = curPos + getOff(curPos) - getOff(prev) + getDec(curPos) - getDec(prev);
					prev = temp;
					//cout << "UPDATED POS: " << curPos << endl;
					//cerr << "GET OFF CUR: " << getOff(curPos) << "GET OFF PREV: " << getOff(prev) << endl;
					//cout << "OVERLAPS: " << getDec(curPos) << " - " << getDec(prev) << endl;
				}
			}
			//cerr << endl;
			//cerr << "ACTUAL INVPOS: " << curPos << endl;

			
			if (s[i] > 0) {
				curPos++;
			}

			//cerr << "ADDING: " << (i - curPos) << endl;
			sum += (i - curPos);
			//cerr << "INCREMENTING: " << curPos << endl;
			incOff(curPos);
			//cerr << "Decrementing: " << i+1 << endl;
			decOff(i+1);
			shoesList[-s[i]].pop(); 
		}
	}
	return sum;
}

Compilation message (stderr)

shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:69:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for (ll i = 0; i < s.size(); i++) {
      |                 ~~^~~~~~~~~~
#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...