제출 #961771

#제출 시각아이디문제언어결과실행 시간메모리
961771raspyArranging Shoes (IOI19_shoes)C++14
100 / 100
139 ms139512 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);
	long long 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;
		upd(n, ns[par].front());
		ns[par].pop();
		if (s[i] > 0 && (i+zm)%2 == 0)
			rez++;
		if (s[i] < 0 && (i+zm)%2 == 1)
			rez++;
		zm++;
	}
	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...