Official

B - Two Flips Editorial by evima Editorial for printing


When card \(i\) is flipped while the side with value \(v\) is facing up, we say that card \(i\) uses the value \(v\).

Here, let us relax the operation in the problem a little. In the original problem, only the values on the currently facing-up sides can be used. Suppose instead that the currently facing-down sides can also be used. However, the final numbers of uses of the front and back sides must be consistent with the original problem. That is, for cards \(1\) and \(2\), the number of times \(A_i\) is used is exactly \(1\) greater than the number of times \(B_i\) is used, and for every other card, the two values are used the same number of times.

Considering the minimum number of operations under this condition, it turns out that we only need to consider operations of the following form.

  • First, card \(1\) uses \(A_1\). Choose a card \(j\) to be used at the same time. Of course, one of the sides of card \(j\) must have \(A_1\) written on it.
  • Let \(x\) be the value written on the other side of \(j\). Card \(j\) uses \(x\). Choose a card \(k\) to be used at the same time.
  • Let \(y\) be the value written on the other side of \(k\). Card \(k\) uses \(y\). Choose a card \(l\) to be used at the same time.
  • \(\vdots\)
  • Finish at the moment when some card uses \(A_2\) and card \(2\) is used at the same time.

This process is easy to understand when represented as a graph. Regard the values as vertices and each card \(i\) as an undirected edge connecting vertices \(A_i\) and \(B_i\). What we want is a walk that starts with \(B_1 \to A_1\) and ends with \(A_2 \to B_2\), and never goes back along the edge it just traversed.

In this walk, visiting the same vertex or edge multiple times is allowed. The only thing not allowed is traversing the edge just used in the reverse direction.

The minimum length of a walk satisfying this condition can be found by BFS.

The issue, however, is whether the walk found in this way can actually be matched with operations in the original problem. In fact, this is always possible. More specifically, the walk determines the pairs of cards to be operated on, and we can prove as follows that among these pairs, there is always one that can be operated on.

Let \(W\) be the number of pairs of cards to be operated on. Next, for each card \(i\), let \(x_i\) be the number of times its currently facing-up side will be used from now on, and \(y_i\) the number of times its currently facing-down side will be used from now on.

We can observe that the following equalities hold.

  • If the currently facing-up side of card \(i\) is the same as the side facing up in the target state, \(x_i=y_i\).
  • If the currently facing-up side of card \(i\) differs from the side facing up in the target state, \(x_i=y_i+1\).
  • \(\sum (x_i+y_i) =2W\)

Now, if the current state does not match the target state, there is at least one \(i\) with \(x_i=y_i+1\). (In fact, by parity, there are at least two.)

From this, we obtain \(\sum x_i > W\).

Now, suppose that no pair can be operated on in the current state. For a pair, let \(v\) be the value that the pair should use. Among the cards in the pair, at most one has \(v\) written on its currently facing-up side.

Summing this over all pairs, the total number of uses of currently facing-up sides over all cards is at most \(W\). This contradicts \(\sum x_i > W\).

Therefore, there is always at least one pair that can be operated on, which proves that the problem reduces to computing the length of a shortest walk.

Thus, this problem can be solved in \(O(N)\) time.

Sample Solution (C++)

posted:
last update: