Problem 5 - Tennis Robot II

Here, we will discuss the solution to Tennis Robot II from AIO 2024.

Solution

Subtask 1: N, M ≤ 1000 and Xi ≤ 1000 for all i

We could try simulating the robot's instructions while storing the current number of tennis balls in each bin. We stop if the robot attempts to take from an empty bin:

Let's call a sequence of M instructions a round. Then, each of the N bins falls into one of three categories:
  1. Type 1: The number of balls in the bin is the same at the end of the round as it was at the beginning. That is, the number of balls that leave the bin during each round is the same as the number of balls that enter the bin.
  2. Type 2: The number of balls in the bin is less at the end of the round than it was at the beginning. That is, the number of balls that leave the bin during each round is more than the number of balls that enter the bin.
  3. Type 3: The number of balls in the bin is more at the end of the round than it was at the beginning. That is, the number of balls that leave the bin during each round is less than the number of balls that enter the bin.
Let's make (and prove) our first observation for this problem. Any bin could cause the robot to crash during the first round. However, after the first round, only type 2 bins can crash the robot. Proof: Now let's make another observation: In subtask 1, we need to simulate at most 1001 rounds. Proof: This gives us a solution: simulate for 1001*1000 = 1001000 operations. If the robot does not crash, then it must run forever.

Subtask 2: Aj ≠ Ak for all j ≠ k

The subtask 1 solution needs to simulate 1000001*200000 = 200000200000 operations in this subtask. This is too slow and we need a faster approach.

Observation: In this subtask, the robot will not crash in the first round. Proof:

Observation: In this subtask, only type 2 bins can cause the robot to crash. Proof: Solution: We simulate the first round and determine all the type 2 bins: This has a time complexity of O(N+M), which is fast enough to solve this subtask.

The below video shows an example of this algorithm.

Full Solution

There are 2 key issues with the subtask 2 solution:

  1. In subtask 2, the robot will never crash during the first round. In subtask 3, it can crash during the first round.
  2. In subtask 2, every type 2 bin decreases by 1 ball each round. In subtask 3, a type 2 bin can decrease by more than 1 ball in each round.
The first issue is simple to fix: when we simulate the first round, we check whether the robot crashes. The second issue is more complicated to fix: Putting this together, we get a solution: This has a time complexity of O(N+M).

Click here to go back.