Editorial for An Animal Contest 7 P4 - Squirrel Shenanigans
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
If or
, performing an operation on
does nothing. If
then performing an operation on
will result in
. If
then performing an operation on
will result in
. After looking at these cases for a bit, we can observe that if
we can never have
. Likewise, if
, we can never have
. We know that in order for
to decrease it must be greater than both of its neighbors, and for it to increase it must be less than both of its neighbors. Combining our previous observation with this, we can see that
can either only increase or only decrease. Therefore,
is either the minimum or maximum value of
where
.
Define as the minimum value of
where
and define
as the maximum value of
where
. If
, we can obviously find
. If
, we know that
. This is because if
, we can't have
since we would have
. If
, we also can't have
. The reason for this is that if we make
,
must be an "increasing" index. In order for
to be increasing it must be less than both of its neighbors. However, in this case,
would be greater than
. The only case we are left with is
which means that
. Since we can easily find
, we can loop through indices in increasing order and apply the above process to find
.
Time Complexity:
Comments