Wi-Fi Setup
InterviewTime limit1sMemory limit128 MB
Cover all cow positions on a line with base stations, where a station covering an interval of length 2r costs A + B*r; minimize total cost.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Math
- Solved
- No attempts yet
Problem
Farmer John's cows () are standing at various positions along the straight path from the barn to the pasture, which we can think of as a one-dimensional number line. Because his cows like to stay in contact with each other, FJ wants to install Wi-Fi base stations at various positions so that all of the cows have wireless coverage.
The cost of a base station depends on the distance it can transmit (its power): a base station of power costs , where is a fixed cost for installing the station and is a cost per unit of transmission distance. If FJ installs such a device at position , it can transmit to any cow located in the range through . A base station with power is allowed, but it only covers a cow located at exactly the same position as the transmitter.
Given the values of and as well as the locations of FJ's cows, determine the least expensive way FJ can provide wireless coverage for all of his cows.
Input
- Line 1: Three space-separated integers , , and ().
- Lines 2 through : Each line contains an integer in the range giving the location of one of FJ's cows.
Output
- Print the minimum cost of providing wireless coverage to all cows.
- The answer is always a multiple of . If it is an integer, print it with no decimal part; otherwise print it followed by
.5(for example,57.5).
Hint
Suppose there are 3 cows at positions , , and , and a base station of power costs . The optimal solution is to build a base station at position with power , covering the cows at positions and , and another station at position with power . The total cost is .