Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
To begin with, let's assume that in the interior of the polygon there are more than two points and not all of them are on the same line. We are only interested in the points on the convex hull from the inner set. For a polygon vertex
we have the situation as depicted:
The green color marks the tangent in ccw (counter-clockwise) direction to the inner hull, index
marks the belonging hull vertex, and index
marks the polygon vertex to which (in ccw direction) we can make the best cut from
. Notice that this will always be the last vertex (starting from
) from the right side of the tangent (looking from
facing
). The area of the polygon from vertex
to
is marked with purple and let's assume it's
. If we take the maximum of these areas for each
(vertex
is determined with vertex
), we will get the solution.
Let's denote with
the vertex adjacent to vertex
in ccw direction. We are interested in vertex
of the inner hull through which the tangent from
is going to pass, and vertex
, the optimal vertex for
. Let's observe the following image:
Vertex
can be found so that we move in ccw direction on the inner hull as long as the next vertex is on the right side of the line from
to the current vertex (if we're looking from
facing the current vertex). In the image, the vertices for which the next vertex is better are marked with red.
We can find vertex
so that we move in ccw direction on the polygon as long as the next vertex is on the right side of the line
, from
facing
.
Notice that in this procedure we can keep track of the current area. When we move from
to
, we subtract the area of the triangle
(marked in the first image). When we look at index
, we add the area of the corresponding triangle in each move (those triangles are separated using dotted lines in the second image).
The complexity of finding the convex hull of an inner set of points is
. After that, finding the optimal cut is
, where
is the number of points on the hull, or
in the worst case scenario. Index
will go through all the points of the outer polygon. Finding the indices
and
can be complex in one transition, but in total it's amortised because both indices can visit a point at most once (
on the polygon,
on the inner hull).
Comments