From http://blog.csdn.net/shuangde800
Question: Click to open the link
Question
There are n hotels on a straight line road. The coordinates are di.
Select K of the N hotels to build a parking lot. A hotel without a parking lot can only use the nearest parking lot.
Ask about the construction scheme with the least total distance and output it.
Ideas
Perform preprocessing first. sum [I] [J] indicates ~ Create a parking lot between J and I ~ The sum of distance from all hotels in J to the parking lot is the smallest.
In hotel I ~ Between J, building at (I + J)/2 is the solution with the minimum total distance.
F [I] [J] indicates the minimum total distance of Building J parking lots for the first I Hotel
So,
F [I] [J] = min {f [k-1] [J] + sum [k] [I], 1 <= k <= I}
As for the output scheme, the DP output scheme generally stores the "Decision" record and recursively outputs it.
Code