Editorial for Yet Another Contest 2 P3 - Maximum Damage
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
First, we can observe that there exists an optimal solution in which every attack involves a subset of exactly enemies.
Second, the order of attacks does not matter.
Third, let's consider if we choose , where
is some composite number. Since
is composite, then we can write
as the product of
and
, where
and
are integers greater than
. Then, instead of attacking a subset and choosing
, we could instead make two attacks, both using the same subset; the first attack would choose
and the second subset would choose
. This would allow us to perform an extra attack than before. Hence, it is optimal to only choose values of
which are prime.
Since each prime is independent, we can solve the problem separately for each prime, and then add the results together.
Consider any prime. Let us construct an array , where
represents the number of times that the prime occurs in the prime factorisation of
. We will ignore arrays
with less than
elements. Then, let us remove zeroes from
and sort
in non-increasing order. Then, each operation consists of selecting
non-zero elements of
and decrementing all of them.
In this subtask, . It is well known that the maximum number of operations for this prime is
. The left expression bounds the answer because the total sum of
decreases by
every operation, and the right expression bounds the answer because at least one of the elements
is decreased in every operation. It can be shown via induction that
operations is achievable (hint: if we choose
and
in our first operation, remove zeroes from
and sort
in non-increasing order, then each of the expressions has decreased by at most
). Also, when
, we don't actually need to perform the sort, since we can calculate
as
.
For this subtask, we can simply loop through all prime numbers, and then loop through all values of in order to generate
.
Time complexity:
Subtask 2
Observe that the total sum of len() across all primes is equal to the number of distinct prime factors of each element in
. Since
, each element in
has at most
distinct prime factors. Therefore, the total sum of len(
) across all primes is at most
.
Therefore, if we can find the value of for each prime quickly, we can solve this subtask efficiently.
It turns out that we can simply modify our sieve of Eratosthenes to find the prime numbers and the value of for each prime number at the same time. Our solution then proceeds as in subtask 1.
Time complexity:
Subtask 3
The only thing left to handle is how to find the number of operations that can be performed on for any
.
One solution is to greedily simulate repeatedly selecting the largest elements in
and decrementing them. Instead of using a priority queue, we should optimise this solution using a frequency array. Details are left as an exercise to the reader.
Alternatively, the number of operations is simply . By similar analysis, the
-th of these expressions bounds the answer because out of the elements from the
-th element onwards, at least
are decremented every operation. Furthermore, we can again use induction to show that this number of operations is achievable.
Finally, we could binary search for the largest such that
.
Time complexity: or
or
Comments
Josh I think the testcases of first and second subtask are weak. My solution gets 60 but I think it fails in this case
5 2
8 8 16 32 32
answer is 10 but mine is 9
https://dmoj.ca/src/4490830