Metal
InterviewTime limit5sMemory limit256 MB
Sort the deposits by x, split them into at most k contiguous groups, and place one horizontal tunnel per group to minimize the largest vertical distance.
- Level
Medium6 of 10
- Topics
- Binary search, Greedy, Sorting
- Solved
- No attempts yet
Problem
A very rare and expensive metal has been found underground. The deposits are spread out through the rock, so the mining plan needs care. The plan is a set of horizontal tunnels joined by vertical elevators. Every elevator connects the right end of one tunnel to the left end of another, and the structure of tunnels and elevators has to be monotone with respect to the ground. That is, starting at the end of the leftmost tunnel and travelling to the end of the rightmost one, you can pass through every tunnel without ever moving left again.
The budget allows only horizontal tunnels, where is a positive integer, so at most vertical elevators can join them. Each deposit then gets its own vertical shaft dropped from a horizontal tunnel. The cost of mining one deposit is the vertical distance between it and the tunnel that serves it. The cost of mining all deposits is the largest of the individual costs.
The deposits are points in the plane, a horizontal tunnel is a horizontal line, and vertical shafts and elevators are vertical lines. The cost of deposit is , the vertical distance to the tunnel that serves it, and the total is . Because of the monotone condition, the deposits served by one horizontal tunnel form a contiguous run once the points are ordered by coordinate.
Given the set of points and a positive integer , write a program that finds the smallest obtainable with at most horizontal tunnels.
Input
The first line contains the number of test cases . Each test case takes two lines. The first line contains the number of deposits and the largest number of horizontal tunnels . (, ) The second line contains integers , , , , , , , the coordinates of , separated by spaces. () No two points share an coordinate.
Output
For each test case, print the minimum value of on its own line. That minimum is always a multiple of , so print it with exactly one digit after the decimal point. A minimum of prints as 2.0 and a minimum of prints as 2.5.