Official

D - Erase Balls 2D Editorial by evima


Here, we assume that \(X = (1, 2, \dots, N)\).

First, let’s consider the operations. Since the order in which the operations are performed does not affect the possibility of operations or the set of remaining points, only the set of points on which we perform operations is important. The condition under which we can perform operations on all points in a set \(s\) is as follows:

  • Let \(s' = (s'_1, s'_2, \dots, s'_m)\) be the sequence obtained by sorting the points in \(s\) in ascending order; then it must satisfy \(Y_{s'_1} > Y_{s'_2} > \dots > Y_{s'_m}\).

The condition under which a point \(i\) remains when we perform operations on all points belonging to set \(s\) is:

  • Let \(t = (t_1, t_2, \dots, t_m)\) be the sequence obtained by sorting the points in \(s \cup \{i\}\) in ascending order; then it must satisfy \(Y_{t_1} > Y_{t_2} > \dots > Y_{t_m}\).

Since it is difficult to find the answer directly, we count the maximal sets of points on which we perform operations. Specifically, we count the sets \(s\) satisfying the following:

  • It is possible to perform operations on all points in \(s\).
  • After performing operations on all points in \(s\), performing an operation on any other point will necessarily remove at least one point.

There is a one-to-one correspondence between maximal sets of operation points and the sets of points that can possibly remain after operations. The maximal sets of operation points can be counted using DP with cumulative sums, with a worst-case time complexity of \(O(N^3)\). With some ingenuity, this can be improved further. Sample Implementation

posted:
last update: