Gadgets Factory
Time limit3sMemory limit256 MB
Given m sorted factories each producing one of n part types, find all coordinates t minimizing the sum over parts of squared distance to the nearest factory of that part, expressed as exact fractions.
- Level
Hard8 of 10
- Topics
- Math, Binary search, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Mr. Smith is a very wealthy gadget enthusiast. When he realized that he cannot buy the gadgets he wants simply because they have not been produced yet, he decided to build his own gadget factory.
The factory will stand on a street called Silicon Road, which is lined with plants that manufacture the high-tech parts gadgets are made of. Silicon Road is perfectly straight and the plants sit right next to it, so we model the road as a number line and each plant as a point on it. We call each such plant a factory.
Producing a gadget requires different parts, and there are factories along the road, each producing exactly one kind of part. If Mr. Smith builds his gadget factory at coordinate , its cost is the sum, over all required parts, of the squared distance from to the nearest factory that produces that part.
Find every coordinate at which this cost is minimized.

Input
The first line contains two integers and (; ).
Each of the next lines contains two integers and : the coordinate of the -th factory and the identifier of the part it produces (; ; ).
Every required part is produced by at least one factory.
Output
Let be the sum, over all parts, of the squared distance from to the nearest factory producing that part. Every point at which attains its minimum is a rational number whose denominator divides .
On the first line print , the number of points at which is minimal. Then print those points in ascending order, one per line, each written exactly as a fraction p/q in lowest terms with , or as the integer p when .