Company: Squarepoint / Hackerrank
Difficulty: medium
There are n servers in the network, arranged in ascending order of their capacity. In the array capacity , the capacity of the i -th server is capacity[i] , where 0 ≤ i < n . The distance between two servers i and j is the absolute difference in their capacities: |capacity[i] - capacity[j]| . For each server i , the closest server of i is the server j ≠ i at the smallest distance from i . The closest server is guaranteed to be unique for every server. To manage the network the following operations can be performed on a server x : Connect x to any server y at a cost of |capacity[x] - capacity[y]| units. Connect x to the closest server of x for a fixed cost of 1 unit. A connection between two servers may be built directly, or routed through any number of intermediate servers; the total cost of a route is the sum of the costs of the individual hops it uses. Connections are two-way: a link built between x and y can be traversed in either direction. Given m queries, each defined by