Official

C - Robots Editorial by evima


Let us consider when the answer is \(0\). It is when we can destroy Robot \(A_i\) at some time for every \(i\) such that \(B_i \geq A_i\). Assume Robot \(A_j\) destroys Robot \(A_i\). If \(B_j \geq A_j\), Robot \(A_j\) should also be destroyed by another robot, so we will have this robot destroy \(A_i\). By repeating this, we can assume \(B_j < A_j\) for Robot \(A_j\), which destroys Robot \(A_i\).

It is optimal to, for every robot such that \(B_j<A_j\), move it \(B_j\) times in ascending order of \(A_j\). After this process, if none of the robots such that \(B_i \geq A_i\) survives, we can save Robot \(0\).

Let us consider rewriting the integers written on the balls. First, without changing the answer, we can change the problem as follows: instead of rewriting the numbers, we delete some balls and add some new balls to minimize \(\max(\)number of deleted balls, number of added balls\()\). We will omit the proof, which is just by case analysis.

First, assume we do not delete any balls. The number of balls that must be added here is the number of robots \(A_i\) satisfying \(B_i \geq A_i\) that none of the robots satisfying \(B_j < A_j\) can destroy.

Second, assume we delete one or more balls. Let \(A_i\) be the greatest number written on the deleted balls. Here, it obviously follows that \(B_i \geq A_i\). We can see that we will delete \(B_i-A_i+1\) of those balls, and Robot \(A_i\) can destroy the robots with numbers less than \(A_i\). Then, we need to add the number of balls equal to the number of robots \(A_k\) satisfying \(B_k \geq A_k\) among the robots that neither Robot \(A_i\) nor the robots \(A_j\) satisfying \(B_j < A_j\) can destroy.

Now, we just need to check these cases above. By using the fact that \(A\) is sorted beforehand, we can solve the problem in a total of \(O(N)\) time.

posted:
last update: