Editorial for COI '07 #3 Tamnica
Submitting an official solution before solving the problem yourself is a bannable offence.
For every room , let the "parent" room be the room before
such that there could be a wall between
and the room. For example, the parent of
is
, the parent of
is
etc. Not all rooms have parents,
and
are examples of this. The first part of any solution is to find a way to determine the parent of a given room, since this is required to parse the input.
There is more than one way to do this, presumably all of which involve some math on the room numbers to achieve complexity better than .
Here is a solution of complexity . Let
be the
"corner" room (corner room
is room
; the others are
). It can be shown that
, with division rounding down. We can use binary search to find the largest
such that
is less than
. The parent of
is then
.
We can also calculate the parent room in . If we group the rooms into groups of sizes
, then group
begins with room
. From this, it is rather obvious that room
is in group
, and from this we can calculate the parent of room
, depending on whether the room is in the first or second half of its group.
The graph is now implicitly constructed, but is still too big to manipulate. Notice that almost all rooms are incidental to exactly two edges, connecting them to the rooms right before and right after them. The exceptions are rooms at both sides of a broken wall – these have three or four edges. We can compress the trivial nodes to make a weighted graph with vertices and
edges, where the vertices are rooms
,
, and all rooms at both sides of a wall. Running Dijkstra's shortest path algorithm on this graph yields a solution of complexity
.
Comments