Official

D - Congruence Points Editorial by en_translator


Actually, this problem can be solved in a total of \(O(N \log N)\) time, in which all the process except for the sorting costs \(O(N)\) time.
First, multiple all the coordinates by \(N\) so that the center of gravity of \(N\) points becomes an integer.
Then, sort the sets of \(N\) vectors \({\bf u}\) relative to the center of gravity, that is, \({\bf u_i}=(X_i-gx,Y_i-gy)\) where \((gx, gy)\) is the center of gravity, by their angular coordinates. Here, the (at most one) vector of length \(0\) is removed. Let \({\bf v}\) be the sorted array. The tie is broken by their lengths.

Then, convert the data of sorted vectors \({\bf v}\) into the data of the following sequence: (\(|{\bf v_1}|,{\bf v_1} \cdot {\bf v_2},{\bf v_1} \times {\bf v_2},|{\bf v_2}|,{\bf v_2} \cdot {\bf v_3},{\bf v_2} \times {\bf v_3}, \dots ,|{\bf v_N}|,{\bf v_N} \cdot {\bf v_1},{\bf v_N} \times {\bf v_1}\)),
where \(\cdot\) denotes the inner product of vectors and \(\times\) the outer product (here \({\bf A} \times {\bf B} =A_x \times B_y - A_y \times B_x\) where \({\bf A}=(A_x,A_y) , {\bf B}=(B_x,B_y)\)).
We can use \(|{\bf v_i}|^2\) instead of \(|{\bf v_i}|\), to keep integerness.

The problem can be solved by checking for the resulting two sequences if one correspond to the other by shifting the elements cyclically by multiple-of-three elements; however the time complexity can be improved with Z-algorithm as fast as \(O(N)\) time.

Sample code (C++)

posted:
last update: