Editorial for IOI '14 P1 - Rail
Submitting an official solution before solving the problem yourself is a bannable offence.
Way: Ad Hoc
Query complexity: 
Time complexity: 
** This problem can be solved by the following steps:
First, we know that station  is C type, and its location. We can query all the other stations' distances from station 
, we call this: 
Second, we sort all the , and obviously, the station 
 with the shortest distance 
 must be the first D type location after station 
.
Third, we process each station one by one according to their shortest distance with station  (that is, the order obtained in second step). For each station processed, we determine its type and location immediately as follows:
3.1 First, we maintain the information (location,id) of the leftmost C type and the rightmost D type as the algorithm proceeds.
3.2 To process the current station , we use two queries, 
query(k,leftmost C type) as , 
query(k, rightmost D type) as . And we also have 
. By some observations, we know that either 
 or 
 is achieved with a 'direct' route (without moving forth and back).
For example, we have only 4 cases to consider:
a. dis[k][L] is a direct route
       (   )   )
       L   k   R
b. dis[k][L] is a direct route
       (       )    )
       L       R    k
c. dis[k][R] is a direct route
    (  (       )
    k  L       R
d. dis[k][R] is a direct route
       (   (   )
       L   k   R
By careful analysis (some if/else conditions), we can get the answer. Sometimes, we may need extra information to resolve the four cases, where we can check with .
Take case (a) for example:
a. dis[k][L] is a direct route
       (   )   )
       L   k   R
the location of  might be 
. Then use this location, we need to check if 
 is reasonable or not.
a.1
       ( ( )   )
       L p k   R
There might be some  (type C) between 
 and 
. So 
 is 
, we need to check whether 
 exists or not. If 
 doesn't exist, then case (a) might be wrong, then try cases (b), (c), and (d) until we find the answer.
Comments