Fast Food
InterviewTime limit1sMemory limit128 MB
Given sorted restaurant positions, choose k of them as depots to minimize the total distance from every restaurant to its nearest depot.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Divide and conquer, Binary search, Greedy
- Solved
- No attempts yet
Problem
The fast-food chain McBurger owns several restaurants along a highway and wants to build several depots, each located at one of its restaurants, to supply the others with ingredients. The depots must be placed so that the total distance between each restaurant and its assigned depot is minimized.
You are given the positions of restaurants along the highway as integers (distances from a fixed reference point on the same highway), together with a number (), the number of depots to build.
The depots are built at the locations of distinct restaurants, and each restaurant is served by the closest depot. The total distance sum
must be as small as possible, where is the position of the depot serving restaurant . Compute the minimum possible total distance sum.
Input
The input contains several fast-food chains. Each chain starts with a line containing two integers and with , , and . The next lines each contain one integer, the positions of the restaurants in increasing order.
The input ends with a chain whose first line is 0 0; this chain is not processed.
Output
For each chain, in the order given, print one line Chain i: S, where i is the chain's 1-based index and S is the minimum possible total distance sum. The terminating 0 0 chain produces no output.