Editorial for The Contest Contest 1 P4 - A Typical YAC Problem
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Let's consider how we can build up a path Josh can take and how this affects its corresponding arrays  and 
.
Initially, Josh is at  so 
, 
 and 
 (
 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 indexwhere
. Then replace
with either the elements
or
. This corresponds to incrementing
,
or
,
by
respectively. This new path has length
.
Then we observe that the above operation is equivalent to the following:
Pickadjacent indices
,
such that at least one of
are nonzero. Then increment
and
by
.
Given an array , how can we check if it can be made by performing the above operations? To check if index 
 is a possible starting point, we first decrement 
 by 
. Then we go from 
 to 
, greedily decrementing adjacent indices 
 until they all become 
. An important observation to make is that each pair of indices 
 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 
, then 
 is a possible starting point.
Subtask 2
We can optimize the solution above to  by precomputing the resultant array after decrementing a prefix or suffix of the array to 
. That is, we let 
 denote the value of 
 after decrementing all adjacent pairs of indices 
 until they become 
. Let 
 denote the value of 
 after decrementing all adjacent pairs of indices 
 until they become 
. Then to check if 
 is a possible starting point, we simply check if 
 (if we can undo the operations to end up with the starting array of 
).
Subtask 3 and 4
We first observe that the minimum length path Josh could take is . Excluding 
, this corresponds to the frequency array 
.
However, this minimum length path may not satisfy all the requirements, since the subarrays , 
 and 
 may have lengths greater than 
. To resolve this, we try adding "zigzags" to the path. To do this, we first define 
. Now consider a monotonic subarray 
 of length 
. If there exists an 
 such that 
 and 
, we can turn the subarray into 
, where the length of the longest contiguous subarray is now strictly less than 
. Hence, we try to add as many zigzags as possible to the path.
To compute this, let  if Josh can go from 
 without visiting more than 
 houses in a row. Similarly we let 
 if Josh can go from 
 without visiting more than 
 houses in a row. To compute 
, we go from left to right and keep track of the rightmost 
 indices 
 (initially 
) of when Josh last changed directions (i.e. when he takes a zigzag). As soon as we find an index 
 where 
, there are two cases. If 
, Josh takes one zigzag so we set 
 to to 
. If 
, Josh takes at least 
 zigzags so we set both 
 and 
 to 
. We then decrement 
 by 
. If 
, then 
 and let 
. Otherwise, any starting point greater than or equal to 
 is impossible so 
. The idea for computing 
 is similar, where we let 
. Then in order for 
 to be a possible starting point, we must also have 
 and 
. The second condition must hold since the subarray 
 is the optimal subarray for Josh to take when he passes by 
 when travelling from 
 as it minimizes the distance between the two zigzags.
Comments