公式

G - Ghost Ants 解説 by en_translator


According to the conditions, ants \(i\) and \(j\) pass each other if and only if the integers \(i,j(i \neq j)\) between \(1\) and \(N\) satisfy \(S_i=\) 1, \(S_j=\) 0, and \( 0 < X_j-X_i \leq 2 \times T\). We need to count such pairs \((i,j)\).

Sorting \(X\) in ascending order and also rearranging \(S\) correspondingly does not change the answer, so we do so. Let \(A\) be the sequence of \(X_i\) for all indices \(i\) with \(S_i=\) 1, and \(B\) be that with \(S_i=\) 0. We assume that \(A\) and \(B\) are sorted in ascending order.

The minimum \(j\) with \(B_j > A_i\) for some \(i\) monotonically increases as \(i\) increases. Also, the maximum \(j\) with \(B_j-A_i \leq 2 \times T\) also increases as \(i\) increases. Therefore, one can use the sliding window trick to find the number of indices \(j\) for each \(i\). Taking their sum yields the answer.

Sample code (C++):

投稿日時:
最終更新: