Editorial for BPC 1 S2 - Train Commute


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: BalintR

Hint

N \log C for the first subtask fits in the query limit. Maybe binary search?

Subtask 1

This subtask was meant to reward all kinds of binary search solutions and linear solutions that use more than 3N queries. The outline of one is provided below.

Finding the position of any station when none of the stations are known can be hard. Therefore, we start by binary searching for the positions of the first and last stations. To do so, we first calculate C - ? 0 C to get the total amount of time saved by using the train as opposed to walking all the way from 0 to C. This is also the distance between the first and last stations. Then we binary search for the smallest value of i such that you save the same amount of time by using the train instead of walking from 0 to i as you did in the initial query. This value of i is the position of the rightmost station. Then we can calculate the position of the leftmost station by subtracting by the value we got in our first query.

After we have the position of the leftmost station, we can do some other type of binary search for each subsequent station. For example, we could take a station and binary search to its right for the smallest position where we can get to it faster than walking. This will be roughly halfway between the current and next station and we can use the result of the final query to determine the exact location of the next station.

Number of queries: N \log C

Hint

If we have determined that there are stations at positions a and b, is there an easy way to check for the position of a station between them?

Subtask 2

If we know there are stations at positions a and b, we can gain a lot of information by making a query from a to \lfloor (a+b)/2 \rfloor. If the result of the query shows that it's the same as walking, then we know there are no stations between a and b. Otherwise, if the query result is x, we know there is a station exactly x units from \lfloor (a+b)/2 \rfloor in some direction. We can make an additional query at \lfloor (a+b)/2 \rfloor + x to check which side the new station is on.

To fully solve the problem, we can use binary search to find the first and last stations. Then we repeatedly use the strategy of querying midpoints on intervals that have not yet been queried. We do this until we have queried the midpoint of every pair of adjacent stations. This can be implemented with recursion or a queue. After the binary search, this solution will use at most 3N queries. 2 queries are required to find each station in the first place, and a total of N more will be used for checking empty intervals.

Number of queries: 3N + \mathcal{O}(\log N)


Comments

There are no comments at the moment.