Editorial for JOI '20 Open P2 - Monochrome Points
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
Under the constraints of Subtask 1, we can try all possibilities ways to draw line segments. Since we can
calculate the intersection number in
time, this subtask can be solved in
time.
Subtasks 2, 3
Let be the positions of the white points in ascending order. Similarly, let
be the
positions of the black points in ascending order. Then, we shall prove that the best way to draw line segments
is to connect
with
for every
for some integer
. Here
is an integer from
to
, and all indices are
considered mod
.
Consider the best way to draw line segments, and assume that is connected with
by a line segment.
First, we shall prove that, for different
and
, the four points
are not located on the circle in this
order (in the clockwise order). In fact, if
are located on the circle in this order, the intersection
number will increase if we connect
with
, and
with
. It contradicts the assumption that we are
considering the best way to draw line segments. Next, we shall show
for every
. If it
is not satisfied for some
, then there exists another black point
between
and
. Now we consider
the positions of the four points
. By the above fact,
are not located in this
order. Similarly,
are not located in this order. Assume that
are located in
this order. Since there does not exist any white point between
and
, the point
must lie between
and
, or between
and
. It contradicts the above fact. There are three remaining cases to consider, i.e.,
the case where
are located in this order, the case where
are located in this
order, and the case where
are located in this order. Similarly, we can derive a contradiction in
every case.
Therefore, the best way to draw line segments is to connect with
, for some integer
. There are
candidates of the value of
. For each
, if we calculate the intersection number in
time, we can solve
the task in
time in total. If we use data structure such as the Binary Indexed Tree or the Segment Tree,
we can calculate the intersection number in
time in the same way as calculating the number of
transpositions of a permutation. The total time complexity is
, and we can solve Subtask 3.
Subtask 4 (Full score solution)
Similarly as in Subtasks 2, 3, we fix an integer and connect
with
. We shall calculate the intersection
number. For an index
, we consider the number of line segments which intersect with the line segment connecting
with
. We consider the two arcs whose endpoints are
. Let
(
) be the number
of points on the arcs except for the endpoints. The number of line segments that intersect with the line segment
connecting
with
is less than or equal to
. We shall prove that the equality actually holds, i.e.
line
segments intersect with the line segment connecting
with
.
Take one of the two arcs such that the number of points in the fixed arc is smaller than or equal to the number
of points in the other arc. We also take a point in the fixed arc. We may assume without loss of generality that
we take a white point. We denote it by . Then, it is enough to prove that the four points
are
not located in this order, in the clockwise order and in the counterclockwise order. We assume that these four
points are located in this order in the clockwise order, and we shall derive a contradiction. We can treat the case
of the counterclockwise order similarly.
By assumption, there are points on the arc from
to
. Since
and
, we have
, i.e., the number of points on the arc is less than or equal to
. Changing the labels of the white
points if necessary, we may assume that
holds. Then, the number of white points on the arc from
to
is
, and the number of black points on the arc from
to
is
. From this, we see that
there are at least
points on the arc from
to
, which is absurd.
Therefore, the number of line segments which intersect with the line segment connecting with
is equal
to the smaller number of points on the arcs between
and
except for the endpoints. (If the two arcs have
the same number of points, we take any one of the two arcs.) By an appropriate case-by-case analysis, we can
calculate this number by a linear function in terms of
and
. Thus we can solve this task if we can add a
linear function on an interval of
for each
. This operation can be done in linear time by a cumulative sum
technique. We need to find the interval where we will add a linear function. We can calculate it in linear time if
we consider
in ascending order. In total, we can solve this task in
time.
Digression
Let be the intersection number for each
. The function
satisfies certain monotonicity condition.
Precisely, if we consider the function
is defined on the integers mod
, the following condition is satisfied.
Let
be the point where
takes the minimum, and
the point where
takes the maximum. Then,
is a monotone non-decreasing function in the interval
, and it is a monotone non-increasing function in the interval
.
To simplify the proof, we assume that the points divide the circle equally and the length of the circumference
is
. For any two points
on the circle, let
be the length of the shorter arc whose endpoints are
. Let
be the distance from
to
in the clockwise direction. Let
be the arc from
to
in the
clockwise direction. (Here the arc
contains
but does not contain
.) If
black points are contiguous
and
white points are contiguous, we can easily check the monotonicity condition. If this is not the case, the
condition
is satisfied for every
. In the following, we may assume this condition is satisfied.
We shall prove that the function satisfies the monotonicity condition imposed on
.
We put
. We need to prove that both
and
are
intervals. It is enough to prove that
satisfies the same monotonicity condition imposed on
. We shall prove
this.
We take a point on the circle and move it. We will consider the variation of the value of
.
Let
be the antipodal points to
, respectively. If we move
to the clockwise
direction by a small quantity
, the variation is
if
is in the interval
, the variation is
if
is in the interval
, the variation is
if
is in the interval
, the variation is
if
is in
the interval
. Hence
is given by the following formula:
Here denotes the length of an arc. For each integer
satisfying
, we define
so that
is the black point closest to the
-th point (including the
-th point itself) in the counterclockwise direction.
Similarly, we define
so that
is the white point closest to the
-th point (including the
-th point itself) in
the counterclockwise direction. If
or
is equal to
, we replace its value by
. For an integer
satisfying
, we define
,
by
,
. Then the above formula for
gives
Here the congruences are modulo . This implies the function
is obtained by adding the constant
on
and a constant function for each
. When we show the monotonicity condition, we can
ignore differences of constant functions. Therefore, it is enough to prove that the function
satisfies the monotonicity condition imposed on .
Arranging the labels of the points, we may assume that, for every , the number of black points in the interval
from the first point to the
-th point is greater than or equal to the number of white points in the interval from
the first point to the
-th point. We can achieve it in the following way. We consider the number of black points
in the interval from the first point to the
-th point minus the number of white points in the same interval. Take
so that this number takes the minimum. Then change the labels so that the
-th point becomes the first point in
the new labeling.
By definition, the values of are the number of black and white points in the interval from the first point
to the
-th point, respectively. By the way to label the points, the inequality
holds for every
.
Moreover, for every
, since
, the inequality
holds. Since
, we have
. Therefore, for every
, we have
.
Hence, for every
, we have
. To prove the monotonicity condition, it is
enough to prove that, for every
, the inequality
holds.
For satisfying
, it follows from
. For
satisfying
,
it follows from
. For
satisfying
, it follows from
.
For
satisfying
and
, if
is satisfied, it follows from
. Finally, for
satisfying
and
, if we put
, then we have
.
Therefore, for every , the inequality
holds, and the assertion is proved.
Comments