Editorial for EGOI '23 P6 - Candy
Submitting an official solution before solving the problem yourself is a bannable offence.
Problem author: Yann Viegas.
The Problem
You are given an array and two integers
and
. In a single
operation, you are allowed to swap any two adjacent elements of the array.
Find the minimum number of operations required so that the first
elements
of the array sum to at least
.
Subtask 1:
,
, 
Here, can only take two values: either
or
.
- Case
: We cannot apply any operation and as
we know that
. Thus it is enough to check whether or not
.
- Case
: We can either swap the two numbers, or not swap them. We can just consider the two cases and see if one of them gives a correct solution.
Overall complexity:
Expected score: 6.
Subtask 2: 
In this subtask, is either
or
for all
. First we will try to find if we can
reach the objective without minimising the number of swaps. Let's say there
are
ones in the array. The largest possible sum would be achieved by moving
all the ones at the beginning of the array. The sum of the first
elements of
the array will then be
. If
, the answer is
NO
. Else, we can
construct an answer by reaching a state where there are at least ones at the
beginning of the array. It is optimal to choose ones in increasing order of their
indices in the array.
Expected score: 19 for just subtask 2, 6 + 19 = 25 for subtasks 1 and 2.
Subtask 3: 
In this subtask, which suggests some sort of backtracking/bitmask
solution.
We can split the array in two parts:
- the left part (first
elements)
- the right part (everything except the first
elements)
Fixing the set of elements that will end in the left part will be enough. Let's say that their indexes (in the original state of the array) are:
The minimum required number of swaps to move them to the left part is:
Indeed, it is never useful to swap and
when
. Furthermore, as
has
to end up in the first position, it needs to be swapped with all the elements to
its left, same argument for
which need to be swapped with all the elements
with indexes in
etc. Thus, all these swaps are necessary. This amount
of swaps is sufficient (applying them is enough to move
to the
left part.
One way of implementing this would be by iterating over bitmasks to iterate
through all the subsets of .
Overall complexity: .
Expected score: 6 + 16 = 22 (it solves subtasks 1 and 3).
Subtask 4: 
The constraints of the problem suggest that we use DP. Also, the solution of subtask 3 presents clearly a process of "choosing/not choosing" elements which is definitely something that could be solved with DP.
The states are:
- The index of the integer we are currently considering
- How many elements we already selected to be in the left part
- The sum of the elements currently in the left part
So will give the minimum number of swaps required to select the first
integers of the left part while having only used the first
integers and such
that their sum is exactly
.
The transitions are pretty simple:
- Either we choose the
th integer to be in the left part
- Or we don't choose it to be in the left part
We use the formula of subtask 3 to update the number of swaps while doing our transitions.
Overall complexity:
Expected score: 6 + 19 + 30 = 55.
Full Task
Let's try to optimise the DP solution we got for the previous subtask. The
thing that makes it slow is that the sum of the integers of the left part can be
extremely high. On the other hand, the value that our DP computes is small.
Indeed, by the formula we found in subtask 3, the number of swaps required to
move all the selected integers to the left part is bounded by . Thus we can
change a bit our states:
- The index of the integer we are currently considering
- How many elements we already selected to be in the left part
- The minimum number of swaps required to move the selected elements to the first part
Given such a state, we would like to maximise the sum of the integers selected
to be in the left part. Thus, will give the maximal sum we can reach
by selecting the first
integers of the left part while having only used the first
integers and such that the minimum number of swaps required to move them
to the left part is exactly
.
Overall complexity:
Expected score: 100
Comments