Space Ant
InterviewTime limit1sMemory limit128 MB
Given N points with distinct x and y coordinates, output the order produced by repeatedly picking the most clockwise remaining point from the current one.
- Level
Medium5 of 10
- Topics
- Geometry, Sorting, Simulation, Greedy
- Solved
- No attempts yet
Problem
The most exciting space discovery of the late 20th century happened in 1999, when scientists tracked down an ant-like creature on the planet Y1999 and named it M11. M11 has a single eye on the left side of its head and exactly three feet, all on the right side of its body. Because of this unusual anatomy it walks under three restrictions:
- It can never turn right.
- It paints the ground red along every step it takes.
- It refuses to step onto ground that is already red, so its path never crosses itself.
Photographs from the Discovery spacecraft show that the plants on Y1999 grow only at special points. After analyzing thousands of images, scientists found a magic coordinate system for these growth points: in the plane with the and axes, no two plants share the same -coordinate or the same -coordinate.
To stay alive, M11 must eat exactly one plant per day. After eating a plant it rests on that spot for the rest of the day, and the next day it walks to another plant and eats it; it can reach a plant at any distance. If it cannot reach a new plant, it dies at the end of the day.
Let be the plant with the smallest -coordinate. M11 begins the walk at the point , heading toward . From then on it may make only counter-clockwise (left) turns, and its trail must never cross itself. Under these rules M11 can always eat every plant. Your task is to output the order in which M11 visits the plants.
The visiting order is uniquely determined by the following construction:
- Start at , the plant with the smallest -coordinate.
- At each step, from the current plant choose the next plant such that every remaining plant lies to the left of the ray from the current plant to ; equivalently, is the most clockwise remaining plant.
- If several remaining plants are collinear on that ray, visit the nearest one first.

Input
The first line contains , the number of test cases (). Each test case begins with a line containing , the number of plants (). The next lines each describe one plant with three integers: its unique index (from to ), followed by its - and -coordinates. The plants are listed in increasing order of their indices. All coordinates are positive integers no greater than , and within one test case no two plants share an - or a -coordinate.
Output
For each test case, print one line: first the number of plants on M11's path (which is always ), then the indices of the plants in the exact order M11 visits them, all separated by single spaces.