Official

A - Two Dimensional Invader Editorial by evima Editorial for printing


Assuming we add the sum of $Z_i$ to the final answer, we may consider that defeating with a sword costs the square of the Euclidean distance minus $Z_i$, and defeating with magic costs $0$. With this transformation, defeating with magic can be interpreted as doing nothing. From now on, we consider the problem in this form.

When moving from point $(x_1, y_1)$ to point $(x_2, y_2)$, a cost of $(x_1 - x_2)^2 + (y_1 - y_2)^2$ is incurred. This increase can be written independently in terms of the change in the $x$-coordinate and the change in the $y$-coordinate. Using this, we rephrase the problem as follows.

  • You may take $(0, y)$ as the initial position, at the expense of increasing the cost by $y^2$.
  • Before defeating the monster with a sword in an event, you may change only the $x$-coordinate. In this case, the cost increases by the square of the change in the $x$-coordinate.
  • When defeating the monster with a sword in an event, the cost decreases by $Z_i$.
  • After defeating the monster with a sword in an event, you may change only the $y$-coordinate. In this case, the cost increases by the square of the change in the $y$-coordinate.

We can perform DP along this. As the state, let $dp[i][j]$ hold the minimum possible cost when you are at $(i, j)$. Letting $\max(X_i) = X, \max(Y_i) = Y$, the time complexity is $O(N(X + Y) + XY)$, which is fast enough.

Supplement

In this problem, the transition from $(x_1, y_1)$ to $(x_2, y_2)$ is realized by the transitions $(x_1, y_1) \rightarrow (x_1, 1), (x_1, 2), \cdots, (x_1, Y)$ and $(1, y_2), (2, y_2), \cdots, (X, y_2) \rightarrow (x_2, y_2)$.

This can be interpreted as combining "push-style DP" and "pull-style DP". In this case, we push to points with the same $x$-coordinate, and pull from points with the same $y$-coordinate.
Here are three similar problems; feel free to use them for practice.

Similar problem (ABC) Similar problem (ARC) Similar problem (QOJ)

Sample Implementation (C++)

posted:
last update: