Editorial for JOI '20 Open P1 - Furniture
Submitting an official solution before solving the problem yourself is a bannable offence.
We may regard the pieces of furnitures initially located in the room are located by operations. It is enough
to consider the solution if there is no furniture in the room in the beginning. More concretely, for each
satisfying
, we add a new operation to the block
in the beginning. We do not output the results of
the new operations.
Subtask 1
In this subtask, it is possible to perform calculations for each operation. Therefore, for each operation,
we temporarily locate a piece of furniture at the block, and decide whether the arrangement is nice or not. If it
is not nice, we remove the piece of furniture.
By breadth first search or the depth first search, it is possible to decide whether the arrangement is nice or not
in time.
Subtask 2 (Full Score)
A move satisfying the condition of a nice arrangement will be called a nice move. If there is a nice move
passing through the block , we put
. Otherwise, we put
.
Assume that we perform an operation to the block .
Case:
.
In this case, there is no move passing through the block
. If we locate a piece of furniture at the block
, a nice move will remain nice. Therefore, the arrangement is nice.
Case:
.
If we locate a piece of furniture at the block
, a nice move passing through
will not remain nice. We need to consider whether there exist other nice moves.
Assume that there exists
satisfying
,
, and
.
In this case, the arrangement will remain nice if we locate a piece of furniture at
. In fact, there exists a nice move passing through
. Since such a move does not pass through
, it remains nice if we locate a piece of furniture at
. For a nice move, the value of
for a block
which we pass through will increase. Hence we do not pass two blocks with the same value of
.
Assume that such
does not exist.
In this case, the arrangement will not remain nice if we locate a piece of furniture at
because every nice move will pass through
. There will be no nice move if we locate a piece of furniture at
. which we pass through will be increase by
. Hence it must pass through
satisfying
. There is no such
other than
in this case.
Therefore, it is enough to manage the values of efficiently. If we locate a piece of furniture at
, we
update as
. It may happen that we need to update as
for some other
. Such
are
detected as follows.
Case:
and
.
In this case, we cannot travel from
to
. Hence there does not exist a nice move passing through
.
Case
and
.
In this case, we cannot travel from
to
. Hence there does not exist a nice move passing through
.
Conversely, if there is no block satisfying the above conditions, the values of are updated correctly. In
fact, for any
with
, either
or
is satisfied. Hence we may move to such
block. Continuing this process, we will arrive at
. Similarly, repeating to move from
to
or
, we will arrive at
. Connecting two paths, we get a nice move.
Therefore, for each update , we need to find adjacent blocks where the value of
should be updated.
Once we update as
, then the value of
will never be
again. Therefore, the time complexity of this
algorithm is
.
Comments