Submission #961770

#TimeUsernameProblemLanguageResultExecution timeMemory
961770raspyArranging Shoes (IOI19_shoes)C++14
50 / 100
119 ms139480 KiB
#include "shoes.h"
#include <queue>
#include <iostream>

#define mx 100000

using namespace std;

queue<int> ns[200005];
int fn[200005];

long long qr(int n, int x)
{
	int rez = 0;
	for (int i = x+1; i; i-=(i&-i))
		rez += fn[i];
	return rez;
}

void upd(int n, int x)
{
	for (int i = x+1; i <= n; i+=(i&-i))
		fn[i]++;
}

long long count_swaps(vector<int> s)
{
	int n = s.size();
	for (int i = 0; i < n; i++)
		ns[s[i]+mx].push(i);
	int rez = 0, zm = 0;
	for (int i = 0; i < n; i++)
	{
		if (ns[s[i]+mx].empty() || ns[s[i]+mx].front() != i)
		{
			zm--;
			continue;
		}
		ns[s[i]+mx].pop();
		int par = s[i]*-1+mx;
		rez += ns[par].front()-i+qr(n, i)-qr(n, ns[par].front())-1;
		// cout << i << ": " << ns[par].front() << " " << i << " " << qr(n, i) << " " << qr(n, ns[par].front()) << "\n";
		upd(n, ns[par].front());
		ns[par].pop();
		// cout << "pr: " << rez << "\n";
		if (s[i] > 0 && (i+zm)%2 == 0)
		{
			rez++;
			// cout << "test1\n";
		}
		if (s[i] < 0 && (i+zm)%2 == 1)
		{
			rez++;
			// cout << "test\n";
		}
		zm++;
		// cout << rez << "\n";
	}
	return rez;
}
#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...