Dividing the Land
Time limit1sMemory limit128 MB
For each test case, split N cities with K-1 evenly spaced vertical or horizontal cuts, avoid cuts through cities, and print the minimum average |count - N/K| as a reduced fraction.
- Level
Medium5 of 10
- Topics
- Sorting, Math, Implementation, Geometry
- Solved
- No attempts yet
Problem
When Kim Sang-geun, emperor of the Changyoung Empire, passed away, the question of how to split his empire among his children remained. The empire is a rectangle that contains cities.
The empire must be split into exactly pieces, using one of only two methods:
- Vertical cuts: draw equally spaced vertical lines to split the empire into vertical strips of equal width.
- Horizontal cuts: draw equally spaced horizontal lines to split the empire into horizontal strips of equal height.
Because every piece must have the same size, the cut positions are fixed for each method. The border of the empire is the smallest axis-aligned rectangle that contains all cities. A cutting line need not have integer coordinates, but it must not pass through a city: if one of the equally spaced lines of a method would land exactly on a city, that method cannot be used.
Each child receives one of the pieces together with the cities inside it. The fair target is , and a child who receives a piece holding cities has an unfairness score of .
Choose the better of the two methods so that the average unfairness score over all children is minimized, and report that minimum as an irreducible fraction.
For example, with cities and children the target is . If the three pieces hold cities, the unfairness scores are with an average of . If instead the cities can be split evenly into , the average becomes .
Input
The input consists of several test cases. The first line of each test case contains the number of cities and the number of children .
Each of the next lines contains the integer coordinates and of a city. Because the coordinates are rounded to the nearest integers, several cities may share the same coordinates.
The last line of the input contains two zeros and must not be processed.
In every test case at least one of the two methods can always be used.
Output
For each test case, print the test case number and the minimum average unfairness score. Write the average as an irreducible fraction A/B, using when the value is an integer. Each line has the format number. A/B.