Alignment of the Planets
InterviewTime limit1sMemory limit1024 MB
Given N grid points, list every triple of points that lie exactly on one line, with IDs sorted within each triple and across the output.
- Level
Easy3 of 10
- Topics
- Geometry, Brute force, Sorting
- Solved
- No attempts yet
Problem
Actually, this problem is about the alignment of () cows numbered to who are grazing in a field that is about units. Every grazing location is on an integer coordinate in a standard grid, with coordinates in the range to .
Bessie looks up and notices that she is exactly lined up with Sara and Julie. She wonders how many groups of three aligned cows exist in the field.
Given the locations of all the cows (no two cows occupy the same location), find every set of three cows that are exactly collinear. Sort the cows in each set by ID number, lowest first. Then sort the sets by their three ID numbers, lowest first, breaking ties by the second and third ID numbers.
Input
The first line contains a single integer .
Each of the next lines contains two space-separated integers, the and coordinates of cow on line .
Output
The first line contains the number of sets of three cows that are exactly collinear. If four cows are collinear, they form four such sets.
Each following line contains three space-separated cow ID numbers of a collinear set. The lines are sorted as described above. If no collinear sets exist, nothing is printed after the first line.