Submission #374888

#TimeUsernameProblemLanguageResultExecution timeMemory
374888peijarXylophone (JOI18_xylophone)C++17
100 / 100
147 ms40940 KiB
#include "xylophone.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;

static int A[5001];
static bool isLess[5001][5001];
static int queries[5001][5001];

// On a |a[i+1] - a[i]|, |a[i+2] - a[i+1]|, max(...)

// a[i] < a[i+2] < a[i+1] : z = x
// a[i+1] < a[i+2] < a[i] : z = x

// 
// a[i+1] < a[i] < a[i+2] : z = y
// a[i+2] < a[i] < a[i+1] : z = y

// a[i+2] < a[i+1] < a[i] : z > x,y
// a[i] < a[i+1] < a[i+2] : z > x,y

void solve(signed N) 
{
	isLess[1][2] = true;
	for (int i(1); i < N; ++i)
		queries[i][i+1] = query(i,i+1);
	for (int i(1); i < N-1; ++i)
		queries[i][i+2] = query(i, i+2);
	for (int i(1); i+2 <= N; ++i)
	{
		int x(queries[i][i+1]), y(queries[i+1][i+2]), z(queries[i][i+2]);
		if (x == z)
		{
			if (isLess[i][i+1])
				isLess[i][i+2] = true, isLess[i+2][i+1] = true;
			else
				isLess[i+2][i] = true, isLess[i+1][i+2] = true;
		}
		else if (z == y)
		{
			if (isLess[i][i+1])
				isLess[i+2][i] = isLess[i+2][i+1] = true;
			else
				isLess[i+1][i+2] = isLess[i][i+2] = true;
		}
		else
		{
			if (isLess[i][i+1])
				isLess[i][i+2] = isLess[i+1][i+2] = true;
			else
				isLess[i+2][i+1] = isLess[i+2][i] = true;
		}
	}
	for (int flip(0); flip < 2; ++flip)
	{
		vector<bool> seen(N+1, false);
		A[1] = 0;
		for (int i(2); i <= N; ++i)
		{
			if (isLess[i-1][i] ^ flip)
				A[i] = A[i-1] + queries[i-1][i];
			else
				A[i] = A[i-1] - queries[i-1][i];
		}
		int posMin(1);
		for (int i(2); i <= N; ++i)
			if (A[i] < A[posMin])
				posMin = i;
		int delta = A[posMin] - 1;
		for (int i(1); i <= N; ++i)
			A[i] -= delta;
		bool ok(true);
		for (int i(1); ok and i <= N; ++i)
		{
			if (A[i] < 1 or A[i] > N or seen[A[i]] or (i == posMin and seen[N]))
				ok = false;
			else
				seen[A[i]] = true;
		}
		if (ok)
		{
			for (int i(1); i <= N; ++i)
				answer(i, A[i]);
			return ;
		}
	}
	assert(false);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...