Data Analysis
InterviewTime limit3sMemory limit1024 MB
Split N points, sorted by x, into K contiguous groups so the total absolute deviation of y from one horizontal line per group is minimized.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Sorting, Heap
- Solved
- No attempts yet
Problem
Alice, a data scientist, is asked to analyze data. Each data point is a 2D coordinate .
The analysis finds line segments parallel to the -axis in the 2D plane. Specifically, you must determine the following values.
- The -coordinates of the segment endpoints . The endpoints of the -th segment are and . Here .
- The -coordinates of the segments .
You cannot choose the values of arbitrarily. To improve accuracy, an error is defined, and the values are chosen to minimize it. The error is defined as follows.
- The error of a data point is , where satisfies .
- The total error is the sum of the errors of all data points.
Alice wants to choose and so that the error is minimized. Find the minimum possible error when these values are chosen appropriately.
Input
The first line contains the number of data points and the number of segments to find , separated by a space.
The next lines each contain a data point , separated by a space. All values are distinct.
Output
Print the minimum analysis error when and are chosen appropriately. An absolute or relative error of up to is accepted.
Constraints
- The input values are all integers.