Editorial for DMPG '19 G3 - A Round Problem
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
To solve this problem, it is necessary to guess the following fact:
Lemma: If every value of the array is either or
(with at least one of each) and there are more
's than
's, there is some
which the first operation can be used on.
This is fairly intuitive and easy to suspect. The proof is included at the bottom for the sake of completeness.
A simple corollary of this lemma is that any array with at most two distinct values can be turned into a constant array in first operations and
second operation. So the first subtask can be solved in this way.
For the full points, fix some number . Construct an auxiliary array
so that
if
and
if
. Observe that the first operation applied to array
is applied in the same manner to array
. From the first subtask's solution, we can either force
to be all
or all
. But this is the same as forcing all the elements in array
to be less than or equal to
or all the elements to be greater than
.
From this observation, it becomes clear that we can binary search to force the array to become constant. In total, at most
second operations are needed and at most
operations are needed in total. For
, the given constraints of
and
are sufficient.
When implementing the solution, it is important to be able to apply the operations to as well. A brute-force,
implementation of this can pass. It is possible to improve this time complexity by using a segment tree to locate elements in
which should be changed. It may be further improved by using a segment tree of sets to quickly compute the median of a range but this is not at all necessary.
Time Complexity:
Proof of Lemma: Let denote the sum of the elements in the subarray from
to
. Let
. Assume for the sake of contradiction that there exists an array satisfying the conditions so that the first operation cannot change any
to a
. This condition is equivalent to
. Its contrapositive,
is also true.
For any index ,
. So at least one of
and
must be positive, so at least one of
,
must be
. This implies that
for any
.
If there is only one such that
, then there is clearly a contradiction and we are done.
Otherwise, consider the pair of indices over all pairs of indices with
such that
is minimized (if there are multiple, pick any such pair). Let
. Note that
since otherwise,
could be turned into a
. Without loss of generality, say
(rather than
). Since
is minimal, clearly
. Now consider
. Since
and
were chosen for
to be minimized, note that these
elements cannot equal to
or the minimality of
would be contradicted. Thus
.
Let ,
,
, and
. Note that the section
describes is diametrically opposite
's section and the section
describes is diametrically opposite
's section. Since
for any index
,
and
. Summing these two inequalities gives
.
From ,
Thus, .
Similarly, from , we can obtain
. Adding these two inequalities gives
. So
This implies that . Also, all the inequalities used to obtain this must have been equalities. This means that
or equivalently,
. Also,
for all
in the sections described by
,
,
, or
.
Since and
, there is some index
where
for which
and
. But for any
between
and
,
. So
and
. From
and
,
This gives . So there is a contradiction and the lemma is true.
Comments