ICPC World Finals 2025

Problem K: Treasure Map

Solved by 72 teams.

First solved after 69 minutes.

Shortest judge solution: 1366 bytes.

The solution consists of two parts. First, we need to understand what the condition of “each unit square has the two evaluations coincide” actually means. It turns out that it means that there are some values ri for 1 ≤ i ≤ n and cj for 1 ≤ j ≤ m such that h(i, j) = ri + cj. This is not hard to prove (or guess).

Now, let’s construct a bipartite graph where we have an edge between x and y if the point (x, y) is one of the points for which we have the height provided. A connected component in this graph determines a set of rows and columns; and we need to assign ri and cj values to all those rows and columns in a way that matches the points in this component. First, let’s assign in any way that makes the point heights match up; we’ll worry about making sure that all heights are positive and minimizing the height in (tx, ty) later.

How to deal with this graph? Notice that we can assign one value arbitrarily – if we increase all the cj and decrease all the ri by the same value, the resulting heights will not change, and so the first assignment can be arbitrary. Once we assign one cj value, all other values will be forced, so we can continue assigning them by BFS or DFS. If we arrive at a contradiction, we can immediately answer impossible. If not, we store the values we found.

Now, the requirement of all heights being non-negative is equivalent to us being able to choose non-negative ris and cjs. One direction is obvious. The other direction follows from the adjustment above – we can move the minimum ri to zero, and if now the minimum cj is negative, then the height at (i, j) is negative. So, we’ll be seeking a solution with non-negative ri and cj that minimizes the height at (tx, ty).

Now, to minimize the height at (tx, ty), we have to consider several cases.