## Editorial for BPC 1 S2 - Train Commute

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

## Hint

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 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 `? 0 C`

to get the total amount of time saved by using the train as opposed to walking all the way from to .
This is also the distance between the first and last stations.
Then we binary search for the smallest value of such that you save the same amount of time by using the train instead of walking from to as you did in the initial query.
This value of 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:**

## Hint

If we have determined that there are stations at positions and , 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 and , we can gain a lot of information by making a query from to . If the result of the query shows that it's the same as walking, then we know there are no stations between and . Otherwise, if the query result is , we know there is a station exactly units from in some direction. We can make an additional query at 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 queries. queries are required to find each station in the first place, and a total of more will be used for checking empty intervals.

**Number of queries:**

## Comments