Editorial for An Animal Contest 3 P5 - Monkey Canoe


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: sjay05

Subtask 1

Notice that a path from a land cell in row 1 to row N is a concatenation of multiple edges each with various lengths. An edge of length l exists between two cells of land if both cells are in the same row/column and the space between them on that row/column is occupied by l puddle cells.

All edges (with a maximum of 2 \cdot NM) can be built by considering rows and columns separately and connecting each land cell to the subsequent cell in the corresponding row/column being iterated.

For ease of storing edges, we map each cell (i, j) with a distinct id.

Process the edges in decreasing order by length l and maintain a bi-directional graph of active edges. After all edges of a certain length l are added to the graph, we can BFS from all land cells in row 1 and track land which cells in row N are visited (again, using only edges with length \ge l). Note that there is a maximum of \max(N, M) different edge lengths.

Note: To find the cells in row N that can be reached with a canoe size being infinitely large, we process all edges with l = 0, and BFS to find the land cells holding this special case.

Time Complexity: \mathcal{O}(NM \cdot \log(NM) + NM \cdot \max(N, M))

Subtask 2

Using the cell ids, maintain a DSU to query for which cells are connected after a certain number of edges have been processed.

For infinitely large canoe sizes, process the edges with l = 0, adding each to the DSU and check which land cells in row N share a set with land cells in row 1 in \mathcal{O}(M \log(NM)) time.

For the remaining edge lengths, process them in decreasing order. After all edges of a certain length l are added to the DSU, we can repeat the \mathcal{O}(M \log(NM)) check for finding which land cells in row N can be connected with a land cell in row 1 using edge lengths \ge l. Note that there are a maximum of \max(N, M) different edge lengths.

Time Complexity: \mathcal{O}(NM \cdot \log(NM) + \max(N, M) \cdot M \log(NM))


Comments


  • 4
    Dan13llljws  commented on Aug. 6, 2021, 12:39 a.m.

    When Testing, I have an alternate solution where I build the graph, and run a variation of dijkstra where I maximize the minimum edge.


  • 2
    Bashca  commented on Aug. 5, 2021, 10:14 p.m.

    Another approach: Build any maximum spanning tree, create a new node connecting first row and run a dfs from it: "minimum edge in the path to a last row cell is the answer".

    Complexity: O(nm\alpha (n m))


  • 6
    hack3rman  commented on Aug. 4, 2021, 5:41 a.m.

    cool task :)