Official

C - Guessing Permutation for as Long as Possible Editorial by antontrygubO_o


Consider any distinct numbers \(X, Y, Z\), and look at where the questions \((X, Y), (X, Z), (Y, Z)\) are (or \((Y, X)\) if \(Y<X\)). Wlog \((X, Z)\) is the last among them. Then, if \(P_X<P_Y\), and \(P_Y<P_Z\), then we would already be able to deduce that \(P_X<P_Z\). Similarly, if \(P_X>P_Y\), and \(P_Y>P_Z\), then we would already be able to deduce that \(P_X>P_Z\). Therefore, for any triple \(X, Y, Z\), in which pair \((X, Z)\) goes the last, \(P_Y\) can’t be between \(P_X\) and \(P_Z\).

Observation 1: If all such claims hold, then we will ask all \(\frac{N(N-1)}{2}\) questions.

Proof: Suppose that for some \((U, V)\) we know that \(P_U<P_V\) before asking the question \((U, V)\). For that, there must be a sequence\((X_1, X_2, \ldots, X_t)\) with \(X_1 = U, X_t = V\), such that we already asked questions about all pairs of adjacent elements here, and learned that \(P_{X_i}<P_{X_{i+1}}\) for all \(1 \le i \le t-1\). Consider the shortest such sequence; clearly, \(t \ge 3\). Now consider the question about the pair \((X_1, X_3)\). If we have already asked it, we would have a shorter sequence. Otherwise, we have that for \(X_1, X_2, X_3\) edge \((X_1, X_3)\) goes after \((X_1, X_2)\) and \((X_2, X_3)\), so \(P_{X_2}\) couldn’t have been between \(P_{X_1}, P_{X_3}\).

Now, consider a complete graph on \(N\) nodes. For each edge \((X, Y)\) with \(X<Y\), let’s color it in white if \(P_X<P_Y\), and in black if \(P_X>P_Y\). Note that each permutation has a corresponding coloring (though not vice versa), and these colorings are different for different permutations.

Now, for every such triple \(X, Y, Z\), we get to know whether edges between \(X, Y\) and \(Y, Z\) have the same color. Record all these conditions, for example, with DSU.

Observation 2: If you get a contradiction (that is, for some two edges, you get that they have to be colored both in the same color and in different colors), the answer is \(0\).

Observation 3: If there is no contradiction, let \(k\) denote the number of connected components in the graph on edges (two edges are connected if there is a condition on the relation of their colors). Then, the answer is just \(2^k\).

Proof: We already showed that if all such claims hold, then we would ask all questions; it remains to show that every coloring corresponds to a valid permutation. For that, we just have to show that there is no cycle in the directed graph corresponding to the coloring. Remember that in a complete directed graph, there is some cycle \(\iff\) exists a directed cycle of length \(3\). Suppose that we get some directed cycle \((P_X \to P_Y, P_Y \to P_Z, P_Z \to P_X)\), wlog pair \((X, Z)\) appears the last among pairs \((X, Y), (Y, Z), (X, Z)\). Then our coloring makes it impossible to get \(P_X \to P_Y, P_Y \to P_Z\) (here \(P_Y\) would be between \(P_X\) and \(P_Z\)).

posted:
last update: