Editorial for Yet Another Contest 5 P3 - Potted Plants
Submitting an official solution before solving the problem yourself is a bannable offence.
If there is a contiguous section of at least three plants that appears twice, then there is also a contiguous section of exactly two plants that appears twice. Thus, we may restate the problem as:
Find the largest palindromic array containing integers between and , such that no subarray of length appears twice.
Since the array is palindromic, we may focus only on deciding the first half of the array. Observe that there cannot be two equal adjacent elements in the first half, since this would imply that this pair of equal adjacent elements also appears in the second half. Thus, there cannot be two equal adjacent elements, with the exception that if the array has an even length, then the middle two elements can be equal. Also, if appears as a subarray in the first half, then cannot appear as a subarray in the first half.
It now becomes clear that the goal is to generate an array such that every pair of elements appears consecutively (in either order) in the first half at most once, and no two adjacent elements are equal; then, we will concatenate this array with its reversed self. For example, if we generate the first half of the array as , then our final array is .
Each pair of consecutive elements in the first half must be one of the possible unordered pairs of elements. Hence, the length of the first half cannot exceed . We will show by construction that this is always possible when is prime.
When , we can simply let the first half be .
Otherwise, we can perform the following procedure, considering elements equivalent modulo :
Start with the array , and initialise .
Then, for all in , we will increase by and then append to the array times.
For example, when , we will generate the array .
This works because for a fixed value of , we will generate all subarrays of length where the first elements is congruent to less than the second element, modulo . This relies on the fact that the GCD of a prime number and any whole number less than it is .
Then, for all pairs of different integers and , if , will be a subarray of the generated array. Otherwise, mod , so will be a subarray of the generated array.
Consider a complete graph with nodes, labelled from to , that is, there is an undirected edge between every pair of nodes. Then, observe that the first half is equivalent to a trail on the graph (i.e. a walk with no repeating edges). Since we want to find the maximum length trail, ideally, we want to find an Eulerian trial (a trail which traverses every edge).
When is odd, every node has an even degree, so an Eulerian trail can easily be found.
However, when is an even number greater than , there are more than nodes with an odd degree, so an Eulerian trail does not exist. Our goal is now to delete the fewest edges such that the graph becomes Eulerian.
We require either or nodes to have an odd degree, and removing an edge can reduce the number of nodes with an odd degree by at most . Hence, the lower bound on the number of deleted edges is .
However, it is always possible to remove edges to obtain an Eulerian graph. We can simply remove the edge between nodes and , the edge between nodes and , the edge between nodes and , and so on. This will result in only nodes and having odd degrees.
To find an Eulerian trail, we can run Hierholzer's algorithm. Due to the nature of this graph, it is also possible to run a DFS on the graph, greedily choosing to move to the neighbour with the lowest label such that the current edge is untraversed.