Official

C - Loong Tracking Editorial by en_translator


Let us try to store information in an array-like data structure, where \(A[i]=\) coordinates of part \(i\).

The following three operations are required:

  • Remove the last element.
  • Insert the first element.
  • Find the \(p\)-th element.

If you use an array to perform these operations, “inserting the first element” costs \(\Omega(N)\) time, making it difficult to finish within the execution time limit.

In languages like C++ where index access against a deque is fast, you can use a deque to get AC (accepted):

Sample code (C++)

In Python on the other hand, index access by dequeue is not fast. In such languages, one can perform all the process fast by reversing the array and not actually “removing the first element.”

Sample code (Python)

Figure

posted:
last update: