Editorial for COCI '14 Contest 3 #6 Kamioni
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us imagine a simplified version of the task where we only have two trucks and one query. An event is defined as a pair  which denotes that the truck with index 
 is turning around at time 
 (in other words, 
 minutes after departure).
After calculating the events and sorting by time, we process one event at a time and keep track of the turnaround time and movement direction for each truck. This information is sufficient to calculate for each moment  which truck is located in the city with a smaller index. The encounter is detected when for times 
 and 
 (consecutive events times) the following holds:
and
or
and
,
- the city in which the first truck is located at moment
,
- the city in which the second truck is located at moment
.
This claim follows from the fact that two lines can have at most one intersection or completely overlap (but they won't because of the remark from the task).
It was possible to get  of points if every query was individually solved in the aforementioned way. This is done by looking at events for the two trucks we wanted to know the number of encounters for in the current query.
When we solve the task for  of points, it becomes clear that, in order to fully solve the task, we will need to find a way to solve multiple queries at once. Let us observe the following algorithm:
- assign the query to just one truck from the pair
 - calculate the events of all trucks and sort the events by time
 - process events in order and while doing so keep track for every truck: the moment when it last turned around, the position it last turned around and its movement direction
 - while processing event 
of truck with index
, for each query assigned to that truck we ask ourselves whether the other truck from the pair is located to the left or to the right of it in time
; let us notice that the condition about encounters from the explanation of the
solution is still valid even though we are observing the relative position of trucks from the query while processing the event for only one truck from the pair (caution is needed only when processing the event after the aliens abduct a truck)
 
Let  be the sum of all 
. If we always assign the query to the truck with the smaller number of cities in its route, we reach a complexity of 
. It is easy to prove the complexity if we split the queries to small and large (a query is large if both trucks from the pair have more than 
 cities in their route, otherwise it is small). We can have 
 small queries and for every small query, we need to determine the relative position of trucks at most 
 times. We can have at most 
 large queries (the ones with more than 
 cities in their route) and for each of them, the second trucks from the query can determine the relative position at most 
 times. Therefore, the complexity of processing small queries is 
 and large 
 which gives us the total complexity of 
.
The task has an alternative solution of the same complexity that splits the trucks to those who have more than  cities on their route and calculates their number of encounters with each of the other trucks and to trucks that have less than 
 cities on their route and calculates only the queries that haven't been calculated while processing the large queries.
Comments