Submission #1252940

#TimeUsernameProblemLanguageResultExecution timeMemory
1252940XXBabaProBerkayXylophone (JOI18_xylophone)C++20
0 / 100
0 ms416 KiB
#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define MP make_pair
#define PB push_back

using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T>
using vec = vector<T>;

template<typename T, int N>
using arr = array<T, N>;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

const int INF = 1e9 + 7;

void solve(int N)
{
	vec<int> ans(N + 1), a(N), b(N - 1);
	for (int i = 1; i < N - 1; i++)
	{
		a[i] = query(i, i + 1);
		b[i] = query(i, i + 2);
	}
	a[N - 1] = query(N - 1, N);

	auto f = [&](int i) {
		int x = a[i - 2];
		int y = a[i - 1];
		int z = b[i - 2];

		if (ans[i - 2] < ans[i - 1])
		{
			if (z != x + y) ans[i] = ans[i - 1] - y;
			else ans[i] = ans[i - 1] + y;
		}
		else
		{
			if (z != x + y) ans[i] = ans[i - 1] + y;
			else ans[i] = ans[i - 1] - y;
		}
	};

	auto g = [&](int i) {
		int x = a[i + 2];
		int y = a[i + 1];
		int z = b[i + 2];

		if (ans[i + 2] < ans[i + 1])
		{
			if (z != x + y) ans[i] = ans[i + 1] - y;
			else ans[i] = ans[i + 1] + y;
		}
		else
		{
			if (z != x + y) ans[i] = ans[i + 1] + y;
			else ans[i] = ans[i + 1] - y;
		}
	};

	for (int i = 1; i < N; i++)
	{
		vec<bool> v(N + 1);
		ans[i] = 1;
		ans[i + 1] = 1 + a[i];
		ans[i - 1] = 1 + a[i - 1];
		if (ans[i + 1] == ans[i - 1])
			continue;
		v[1] = v[ans[i + 1]] = v[ans[i - 1]] = 1;
		bool o = 1;
		for (int j = i + 2; o && j <= N; j++)
		{
			f(j);
			if (ans[j] < 1 || ans[j] > N || v[ans[j]])
			{
				o = 0;
				break;
			}

			v[ans[j]] = 1;
		}
		for (int j = i - 2; o && j >= 1; j--)
		{
			g(j);
			if (ans[j] < 1 || ans[j] > N || v[ans[j]])
			{
				o = 0;
				break;
			}

			v[ans[j]] = 1;
		}

		if (o) break;
	}

	for (int i = 1; i <= N; i++)
		answer(i, ans[i]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...