Submission #501490

#TimeUsernameProblemLanguageResultExecution timeMemory
501490fusabileArranging Shoes (IOI19_shoes)C++17
10 / 100
1025 ms3108 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dbg.h"
#else
#define dbg(...)
#endif
#define sz(s) (int)(s).size()
#define all(x) (x).begin(), (x).end()

typedef long long ll;
const int inf = 1e9;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

ll count_swaps(vector<int> S)
{
	int n = sz(S);
	ll ans = 0;
	for (int i = 0; i < n; i += 2)
	{
		//decide which shoes to put here
		vector<array<int, 3>> best; // best[i] = {left_index, right_index, swap_count}
		int j;
		for (j = i+1; j < n; j++)
			if (S[j] == -S[i]) break;
		assert(j < n);
		if (S[i] < 0)
			best.push_back({i, j, j - i - 1});
		else
			best.push_back({j, i+1, j - i});
		i++;
		if (abs(S[i]) != abs(S[i-1]))
		{
			for (j = i+1; j < n; j++)
				if (S[j] == -S[i]) break;
			assert(j < n);
			if (S[i] < 0)
				best.push_back({i, j, j - i + 1});
			else
				best.push_back({j, i+1, j - i + 2});
		}
		i--;
		sort(best.rbegin(), best.rend(), [](auto &x, auto &y) { return x[2] < y[2]; });
		auto [x, y, z] = best[0];
		ans += z;
		for (int k = x; k > i; k--)
			swap(S[k], S[k-1]);
		for (int k = y; k > i+1; k--)
			swap(S[k], S[k-1]);
	}
	return ans;	
}

#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...