Polyline Simplification
Time limit5sMemory limit512 MB
Repeatedly remove the interior point whose triangle area is smallest, breaking ties by original index, and report each removal index.
- Level
Medium7 of 10
- Topics
- Heap, Linked list, Simulation, Geometry
- Solved
- No attempts yet
Problem
Mapping applications represent the boundaries of countries and cities as polylines, which are connected sequences of line segments. Fine details have to be shown when the user zooms in, so such a polyline often contains a very large number of segments. When the user zooms out, those details do not matter, and processing and drawing a polyline with that many segments is wasteful. This problem uses one polyline simplification algorithm that approximates the original polyline with a polyline that has fewer segments.
A polyline with segments is described by points , where the th segment joins and . Removing an interior point () simplifies the polyline: the segments and are replaced by the single segment . Choose the point to remove like this. For every interior point , compute the area of the triangle formed by , and (the area is when the three points are colinear), and remove the point whose triangle has the smallest area. When several points have the smallest area, remove the one with the smallest index in the original polyline. Apply the same rule to the resulting polyline again, until the desired number of segments is left.
The figure below shows one step.

The original polyline is at the top. In the middle, the area of the triangle formed by , and is measured, and is removed when that area is the smallest among all such triangles. The bottom shows the polyline after is removed.
Input
The first line contains two integers () and (). Each of the next lines describes one point, giving through in order. A line holds the and coordinates of the point, both integers between and inclusive. The points are strictly increasing in lexicographic order. That is, for every , either , or and .
Output
Print lines. On the th line print the index of the point removed in the th step of the algorithm above. Use the index the point has in the original polyline.