Official

E - Replace Triplets Editorial by evima


The problem is trivial if \(A\) is a permutation or the number of distinct values in \(A\) is one, so let’s consider the other cases.

Without loss of generality, we can assume that \(A_1 \neq A_N\). (☆)

First, since the initial \(A\) is not a permutation, we need to be able to perform at least one operation on \(A\).

Also, when we consider the run-length compressed sequence \(A'\), we cannot delete elements of \(A'\) through operations. Therefore, if the same value appears multiple times in \(A'\), i.e., the values are separated, we cannot create a permutation. (Note: Since this problem considers a circular arrangement, dealing with the ends can be tricky. Thus, we simply the discussion by appropriately rotating at the beginning and assuming (☆).)

From now on, let’s consider inputs where at least one operation can be performed and the values are not separated.

Consider the case where the number of distinct values in \(A\) is \(N - 2\), for example, \(A = (1, 1, 1, 2, 3, 4)\). In this case, in the final operation, we replace two of the triplet with non-existing values (in this example, \(5\) and \(6\)). In prior operations, we need to maintain the triplet, so the possible operations are changing to \((1, 2, 2, 2, 3, 4)\) or \((4, 4, 1, 2, 3, 4)\). By repeating this operation until we have a triplet of \(1\) again, the positions of each element in the sequence are shifted by \(2\). That is, a \(2\)-shift can be achieved. Finally, we insert the remaining two values at positions where their (circular) distance is \(2\) or less.

Summarizing the discussion so far, the permutations \(P\) that can be generated are all those satisfying the following conditions:

  • The order of values existing in \(A\) is preserved in \(P\).
  • The distances of the two missing values in \(P\) is at most \(2\).
  • Since we can only shift positions by \(2\), when \(N\) is even, only half of the permutations satisfying the above conditions are possible. (This discussion breaks down only when \(N = 4\), making it a corner case.)

When the number of distinct values in \(A\) is \(N - 3\) or fewer, by repeating appropriate operations, we can insert missing values arbitrarily and adjust the positions of a triplet of a certain value freely. However, in the final insertion, the distance between the remaining two values needs to be \(2\) or less. In this case, the permutations \(P\) that can be generated are all those satisfying:

  • The order of values existing in \(A\) is preserved in \(P\).
  • There exists a pair of two missing values such that their distance in \(P\) is \(2\) or less.

Based on the above discussion, let’s proceed to count the number of permutations. The most challenging point in this counting is:

  • There exists a pair of two missing values such that their distance in \(P\) is \(2\) or less.

First, we use the complement event to restate the condition as:

  • For any pair of missing values in \(A\), their distance in \(P\) is \(3\) or more.

Since dealing with distances on a circle is troublesome, we consider both cases for placing the missing values at the ends of \(P\) or not and then consider linear distances in the sequence. Furthermore, if we decide the order of the missing values at the end and insert indistinct values, we need to count sets of insertion positions that are not adjacent, which can be calculated using binomial coefficients. The time complexity is \(\mathrm{O}(N)\).

posted:
last update: