Editorial for CCC '25 S3 - Pretty Pens
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For this subtask, there are no modifications. For every colour , we
can order all pens of that colour by their prettiness. Let
be the
ID of the pen with the highest (maximum) prettiness value with colour
and let
be ID of the second highest. Note that
may not
exist for some
.
If we do not change any pen's colour, then the answer is
where
is the prettiness of pen with
ID
. This can be stored in a variable and used for further
calculations. Observe that we can always achieve the optimal answer by
only changing the colour of
. And the resulting answer if we change
to have colour
is
However,
we cannot afford to try to change the colour of every pen
to every
colour
as there are up to
different
pairs to
considers. However, we can notice that it is always optimal to pick the
colours with the smallest
or second smallest
. This
reduces to
pairs to check, which will run in time. This solution
runs in
time.
Subtask 2
There is only 1 colour for this subtask. Thus, the answer is the pen
with the largest prettiness. Because there are updates, we can maintain
this information in a dynamic data structure such as a balanced binary
search tree (BBST) such as std::set<std::pair<int, int>>
(the pair
represents a pen by its prettiness and pen ID) or with a
std::multiset<int>
in C++ where the pens are ordered by their
prettiness. To update, we also need a mapping from pen ID to their
prettiness, which can be stored in an array. If implemented correctly,
this runs in time.
Subtask 3
Now the pens could be of 2 colours. Notice that we are always able to
pick any two pens. Thus, we can always pick the two pens with the
highest prettiness values. We employ a similar solution to the second
subtask by maintaining the appropriate data structures and summing the
two highest prettiness values instead of just taking the highest
prettiness value. This runs in the same time as the intended Subtask 2
solution, time.
Subtask 4
Now, there can be up to 10 colours. Here, we can solve this by using
Subtask 1's observations and Subtask 3's data structures by applying
Subtask 1 to every query. This results in a time complexity of
.
Subtask 5
This subtask was meant to award contestants with the correct idea as Subtask 6, even if they have implemented parts incorrectly.
Subtask 6
For the final subtask, we can further reduce the pairs to check to
two cases:
- We change the colour of no pens.
- We change the colour of the
with the largest
to the colour of
with the smallest
.
Note that the with the largest prettiness is the same as the pen
that is not a
with the largest prettiness. We can find
with
largest
and pen
with smallest
by having these
data structures and values:
- A BBST of the pens of the form
ordered by prettiness
- A BBST of every other pen, which is not a
ordered by prettiness
- A BBST for every colour
, storing every pen of the same colour
ordered by prettiness
- An array of pens storing their prettiness and colour, indexed-by their ids
- A value representing the sum of prettiness of the first BBST (Let
this value be
)
After every query, we must quickly update all data structures. Now the
answer is
If all the data structures are maintained properly, this solution runs
in
time.
Comments