Editorial for The Contest Contest 1 P4 - A Typical YAC Problem


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: rickyqin005

Subtask 1

Let's consider how we can build up a path Josh can take and how this affects its corresponding arrays p and f.

Initially, Josh is at S so p = [S], m = 1 and f = [0,\ldots,0,1,0,\ldots,0] (f_S is the only nonzero element). Observe that we can extend Josh's path by performing the following operation as many times as needed:

Pick an index i where 1 \le i \le m. Then replace p[i] with either the elements p[i],p[i]+1,p[i] or p[i],p[i]-1,p[i]. This corresponds to incrementing f[p[i]+1], f[p[i]] or f[p[i]-1], f[p[i]] by 1 respectively. This new path has length m+2.

Then we observe that the above operation is equivalent to the following:

Pick 2 adjacent indices i, i+1 such that at least one of f[i],f[i+1] are nonzero. Then increment f[i] and f[i+1] by 1.

Given an array f, how can we check if it can be made by performing the above operations? To check if index i is a possible starting point, we first decrement f[i] by 1. Then we go from 1 to N, greedily decrementing adjacent indices (1,2),(2,3),\ldots,(j,j+1),\ldots,(N-1,N) until they all become 0. An important observation to make is that each pair of indices (j,j+1) must be decremented at least once, since when constructing the array, at least one of the elements in the pair had to already be nonzero. If these conditions are met and the ending array is [0,0,\ldots,0,0], then i is a possible starting point.

Time Complexity: \mathcal{O}(N^2)
Subtask 2

We can optimize the solution above to \mathcal{O}(N) by precomputing the resultant array after decrementing a prefix or suffix of the array to 0. That is, we let \text{pref}[i] denote the value of f[i] after decrementing all adjacent pairs of indices (1,2),(2,3),\ldots,(i-1,i) until they become 0. Let \text{suff}[i] denote the value of f[i] after decrementing all adjacent pairs of indices (N,N-1),(N-1,N-2),\ldots,(i+1,i) until they become 0. Then to check if i is a possible starting point, we simply check if \text{pref}[i]+\text{suff}[i]-f[i] = 1 (if we can undo the operations to end up with the starting array of [0,\ldots,0,1,0,\ldots,0]).

Time Complexity: \mathcal{O}(N)
Subtask 3 and 4

We first observe that the minimum length path Josh could take is [S,S-1,...,2,1,2,...,N-1,N,N-1,...,S+1,S]. Excluding p_1 = S, this corresponds to the frequency array f_m = [1,2,2,2,\ldots,2,2,2,1].

However, this minimum length path may not satisfy all the requirements, since the subarrays [S,S-1,\ldots,2,1], [1,2,\ldots,N-1,N] and [N,N-1,\ldots,S+1,S] may have lengths greater than K. To resolve this, we try adding "zigzags" to the path. To do this, we first define f^{\prime}[i] = f[i]-f_m[i]. Now consider a monotonic subarray [x,x+1,...,y-1,y] of length |y-x+1|. If there exists an z such that x+1 < z < y and f^{\prime}[z-1], f^{\prime}[z] > 0, we can turn the subarray into [x,x+1,...,z-1,z,z-1,z,...,y-1,y], where the length of the longest contiguous subarray is now strictly less than |y-x+1|. Hence, we try to add as many zigzags as possible to the path.

To compute this, let \text{left}[i] = \text{true} if Josh can go from i \to 1 \to i without visiting more than K houses in a row. Similarly we let \text{right}[i] = \text{true} if Josh can go from i \to N \to i without visiting more than K houses in a row. To compute \text{left}[1], \ldots, \text{left}[N], we go from left to right and keep track of the rightmost 2 indices a,b (initially a = b = 1) of when Josh last changed directions (i.e. when he takes a zigzag). As soon as we find an index i where f^{\prime}[i-1], f^{\prime}[i] > 0, there are two cases. If f^{\prime}[i-1] = 1, Josh takes one zigzag so we set b to to i-1. If f^{\prime}[i-1] \ge 2, Josh takes at least 2 zigzags so we set both a and b to i-1. We then decrement f^{\prime}[i-1], f^{\prime}[i] by f^{\prime}[i-1]. If i-\min(a,b) < K, then \text{left}[i] = \text{true} and let \text{lastl}[i] = \max(a,b). Otherwise, any starting point greater than or equal to i is impossible so \text{left}[i], \text{left}[i+1], \ldots, \text{left}[N] = \text{false}. The idea for computing \text{right}[1], \ldots, \text{right}[N] is similar, where we let \text{lastr}[i] = \min(a,b). Then in order for i to be a possible starting point, we must also have \text{left}[i] = \text{right}[i] = \text{true} and \text{lastr}[i] - \text{lastl}[i] < K. The second condition must hold since the subarray [\text{lastl}[i], \text{lastl}[i]+1, \ldots, \text{lastr}[i]-1, \text{lastr}[i]] is the optimal subarray for Josh to take when he passes by S when travelling from 1 \to N as it minimizes the distance between the two zigzags.

Time Complexity: \mathcal{O}(N)

Comments

There are no comments at the moment.