Editorial for APIO '15 P2 - Jakarta Skyscrapers


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.

Subtask 1

Due to the small constraint of this subtask, it is possible to do a breadth-first search, with the current positions of the doges as the state. From a state, there can be at most M3 transitions, since each doge can either move left, right, or stay. The number of states is MN+1, since each doge can either have the news (N possible positions) or it does not have the news (it must be in its starting building). The complexity of this solution is O(MN+4).

Subtask 2

Note that for doge 1 to find the news, there must be a sequence of doges x1,x2,,xk such that doge xi got the news from doge xi1, x1=0 as the first doge to get the news, and xk=1. By using this observation, we can reduce the state to only keep track of one doge.

Now, we can optimize the BFS to only keep track of the position of the current doge, and the power of the current doge. The state becomes (position,power), and the number of vertices is at most NM. From each vertex, we can have two edges:

  • Move: Connect (position,power) to (position+power,power) and (positionpower,power).
  • Move and pass the news to another doge: Connect (position,power) to (position+power,new_power) and (positionpower,new_power).

From each vertex, there can be at most two edges of the first type and M edges of the second type. The total number of edges is O(N2M). Therefore, the complexity of this solution is O(NM2).

Subtask 3

We can optimize this further by transforming the problem to a weighted graph. If we consider a series of jumps from one doge to another as one single edge, we can reduce the state to just (doge). In other words, connect doge1 to doge2 with an edge of weight w, if doge2 can be reached by doge1 in w jumps. In the worst-case scenario, the graph is complete with O(M2) edges. By running Dijkstra's algorithm on this graph, we get an O(M2logM) solution.

Subtask 4

The problem with the solution of Subtask 2 is that if there are X doges with different powers in building Y, every jump that can reach Y will need to connect with at least X vertices. To reduce this number, we introduce a dummy entry vertex for each building. Every jump that can reach this vertex will connect to this dummy vertex with cost 1, and this dummy vertex will connect to every doge in the building with cost 0. In other words, if we write this new dummy vertex as (position),

  • Connect (position,power) to (position+power,power) and (positionpower,power) with an edge of weight 1.
  • Connect (position,power) to (position+power) and (positionpower) with an edge of weight 1.
  • Connect (position) to (position,power) for each doge with power power in position with an edge of weight 0.

Now, the number of vertices is still the same (NM), but the number of edges is reduced to N edges from (position) vertices, and 2NM edges from (position,power) vertices. The complexity is O(NM).

Subtask 5

We still need to cut the number of vertices for this subtask. To do this, we combine the idea from subtask 3 and subtask 4:

Create (position,power) vertices only for powerN. From a (position) vertex, we connect to (position,power) vertex for doges with powers that are less than or equal to N. If the power of the doge is more than N, loop through the possible positions that this doge can jump to, either directly or indirectly, and connect to the entry vertex of those buildings. Now, the number of vertices is at most O(MN).

The number of edges can be analyzed as follows:

  • There can only be two edges from a (position,power) vertex connecting it to another (position,power) vertex. The total number of these edges is at most 2NN.
  • There can be one edge from each (position,power) vertex connecting it to a (position) vertex. The total number of these edges is at most NN.
  • The number of edges from a (position) vertex to a (position,power) vertex is at most N for each position, since there are only N different values of power it can connect to. The total number of these edges is at most NN.
  • The number of edges from a (position) vertex to another (position) vertex is bounded by the number of jumps for each doge. Each doge with power greater than N can only jump at most N times. The total number of these edges is at most MN.

Therefore, the number of edges is O((M+N)N).

Running Dijkstra's algorithm in this graph gives an O((M+N)Nlog(M+N)) solution.


Comments

There are no comments at the moment.