Editorial for JOI '23 Open P3 - Garden
Submitting an official solution before solving the problem yourself is a bannable offence.
Author: Yuto Watanabe
Overall consideration
If a cell is represented by , the values of
and
are called the
-coordinate and the
-coordinate,
respectively. The values of
, which determine the position of the garden, are called the left end, the right
end, the upper end, and the lower end, respectively.
The artworks placed on the cells ,
are the same, and the artworks placed on the cells
,
are the same. So, there is no reason to make a garden whose horizontal width exceeds
or vertical
height exceeds
. Therefore, it is enough to consider the problem in the
-dimensional torus in the range
,
(namely, we consider
and
are adjacent from left to right, and
and
are adjacent from down to top). Then, the conditions for artworks of type A become
and
, and the conditions for artworks of type B become
or
.
Subtask 1 (15 points)
Since it is bothering if the conditions contain the logical operator "or", we would like to remove them. For
every artwork of type B, we check whether it satisfies one of or
. Then the conditions
on the garden become a conjunction of conditions connected by the logical operator "and". Therefore, for each
-coordinate, we need to solve the following problem (we call it Problem X):
- Assume that the integers between
and
are placed on a circle. We choose an interval on a circle so that some of the integers should be contained in the interval. Calculate the minimum length of the interval.
Problem X can be solved in time . Combining with the time to check every artwork, the total time
complexity is
.
Subtask 2 (6 points)
We check all the left ends, the right ends, the upper ends, and the lower ends. For every artwork among the
artworks, we need to check whether it satisfies the conditions. The total time complexity is
.
Subtask 3 (8 points)
If we fix the left end, the right end, and the upper end, we need to consider the condition of the form "the
lower end should be greater than " for every artwork among the
artworks. It is easy to calculate the
optimal lower end. The total time complexity is
.
Subtask 4 (16 points)
If we fix the left end and the right end, we need to solve Problem X for the -coordinates for every artwork
among the
artworks. The total time complexity is
.
Subtask 5 (30 points)
If we fix the left end, we decrease the right end one-by-one from . Then, in Problem X, the following queries
are given successively:
- Remove an integer from the set of integers "which should be contained in the interval."
- Calculate the minimum length of the interval satisfying the conditions.
In other words, we need to solve a dynamical version of Problem X. This can be processed in time per
query using a data structure such as linked list.
If we shift the left end, we need only to consider artworks which have influence. For each fixed value of the
right end, each object is considered at most once. Therefore, we can solve the task in time.
Subtask 6 (25 points)
In Subtask 5, we consider all artworks which have influence when we shift the left end. If we can only
consider artworks such that the number of integers "which should be contained in the interval" is decreased by
one, the total time complexity is reduced to . This is possible if we appropriately keep track of
- the minimum value of
for artworks of type B satisfying
for each
.
when we check all possible values of the right ends (concretely, we increase one-by-one from ).
Comments