Submission #1021747

#TimeUsernameProblemLanguageResultExecution timeMemory
1021747induwara16Arranging Shoes (IOI19_shoes)C++17
30 / 100
1069 ms1048576 KiB
#include "shoes.h"

#include <bits/stdc++.h>
using namespace std;
#define itr(a) a.begin(), a.end()

typedef vector<int> vi;
typedef unordered_map<int, int> mii;

void move(mii &chain, mii &rev, int a, int b)
{
	chain[rev[a]] = chain[a];
	rev[chain[a]] = rev[a];

	chain[a] = chain[b];
	rev[chain[b]] = a;

	chain[b] = a;
	rev[a] = b;
}

int cost(mii chain, int a, int b, int c = 0)
{
	if (a == b)
		return c;

	c++;
	return cost(chain, chain[a], b, c);
}

long long count_swaps(vi s)
{
	int n = s.size(), l = 0;
	long long c = 0;

	mii chain, rev;
	unordered_map<int, priority_queue<int>> ref;

	chain[-1] = 0;
	rev[0] = -1;

	for (int i = 0; i < n; i++)
	{
		if (i != n - 1)
		{
			chain[i] = i + 1;
			rev[i + 1] = i;
		}

		ref[s[i]].push(-i);
	}

	for (int i = 0; i < n / 2; i++)
	{
		int ln = s[l], li = l, cst = 0;

		li = -ref[-ln].top();
		ref[ln].pop();
		ref[-ln].pop();

		if (ln < 0)
		{
			c += cost(chain, l, li) - 1;
			move(chain, rev, li, l);
			l = chain[li];
		}
		else
		{
			c += cost(chain, l, li);
			move(chain, rev, li, rev[l]);
			l = chain[l];
		}
	}

	return c;
}

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(vi)':
shoes.cpp:55:26: warning: unused variable 'cst' [-Wunused-variable]
   55 |   int ln = s[l], li = l, cst = 0;
      |                          ^~~
#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...