Editorial
Problem Authors: | Matviy Aslandukov, Roman Bilyi, and Anton Tsypko |
Problem Prepared by: | Matviy Aslandukov |
Editorial by: | Matviy Aslandukov |
First, it is necessary to consider the case when . In this case, we cannot decrease any number, so if there is at least one number , the answer is . Otherwise, all numbers are already non-positive, so we can perform operations.
Now let's consider the solution for each test group for :
Block 1
In the first group, the value of , so for one operation, we can simply decrease any number by . To make the value non-positive, it is necessary to perform with it at least operations. Therefore, the minimum number of operations needed to make all numbers non-positive is .
Solution: [link to the solution]
Block 2
To solve this group, it was necessary to determine the criterion for making all numbers non-positive exactly in operations. For this, let's try to reduce the problem to the case of . Specifically, let's say that for one operation, we increase all numbers by , and decrease one number by . It can be seen that this operation is equivalent to the operation from the condition. With this definition, we can say that after performing operations, all will increase by . After this, to make the number non-positive, it is necessary to perform with it at least operations. Therefore, we get a simple criterion: all numbers can be made non-positive in exactly operations if and only if .
It may seem that a simple binary search for the answer can be used. But such a solution is incorrect, as there is not always an answer that uses exactly operations, even if there is an answer that uses exactly operations. For example, on the test with , all numbers can be made non-positive in operations, but not in .
It can be proven that when the answer exists, it does not exceed , where . Therefore, it is possible to check all possible answer options from 0 to , using the described criterion. To check the criterion, it is necessary to perform operations, so the total complexity of this solution is .
Solution: [link to the solution]
Block 3
We will maintain the current answer , which initially equals zero, as well as the array , where denotes the number of operations performed on position . On the next iteration of the algorithm, we will iterate through all positions . If the inequality is satisfied, it is necessary to increase and by . If the value of does not increase on the current iteration of the algorithm, the answer has been found. If becomes greater than , it is necessary to output . It can be proven that this solution works in time. It can also be noted that this solution will perform exactly one iteration on the tests of the first group and will work in time.
Solution: [link to the solution]
Block 4
First, let's understand how the constraint helps. Firstly, the criterion check described in the second point is slightly simplified: . Secondly, it can be noted that when all , each position will need to perform at least one operation. This means that if , the answer is . Indeed, suppose that there is an optimal answer. Since each position has been operated at least once, it is possible to reduce the number of operations at each position by . The answer will remain valid, as each will not increase. Therefore, we have obtained a solution with fewer operations, which contradicts the assumption of the optimality of the answer.
Now let's consider the case when . Suppose that exactly operations can make all numbers non-positive. Then it can be seen that exactly operations can also make all numbers non-positive. Indeed, it is possible to simply perform one more operation at each position , after which the values at all positions will decrease by . Therefore, it is possible to iterate through the remainder when dividing by , and perform a binary search on numbers with such a remainder. The complexity of this solution is .
Solution: [link to the solution]
Block 5
Unlike the previous solution, we will iterate through the remainders () from the answer when dividing by in a random order. We will also maintain the minimum found answer among all the remainders considered previously. Then, before starting the binary search on the current remainder, we will check if the answer is improved. To do this, it is necessary to check the previously described criterion for the maximum number less than with a remainder of . Only if the criterion is satisfied, the binary search needs to be started. It can be proven that in this solution, the binary search will be started times, and therefore the total complexity is .
Solution: [link to the solution]
Block 6
To speed up the previous solution, it is sufficient to use the idea described in point 5. The complexity of this solution is .
Solution: [link to the solution]
Block 7
To speed up the previous solution, it is sufficient to use the idea described in point 5. The complexity of this solution is .
Solution: [link to the solution]
Block 8
For convenience, let's assume that all numbers are sorted in descending order. In the last four blocks, the numbers could be negative, but in reality, this does not complicate the problem very much. Indeed, let's denote by the maximum number not greater than such that . Then it can be seen that at least one operation could have been performed with at most positions (a proof similar to the one from point 4 can be given). This means that for the last positions, no operations were performed at all.
Then for the first numbers, the problem can be solved in the same way as in group 4, and then it is necessary to check that the last numbers remain non-positive after performing the found number of operations.
Solution: [link to the solution]
Block 9
To speed up the previous solution, it is sufficient to use the idea described in point 5. The complexity of this solution is .
Solution: [link to the solution]
Block 10
To speed up the quadratic solution, it is necessary to learn how to quickly check the criterion described in point 2. The difference from the solution of the sixth group is that can be less than zero, and therefore some should not add negative values to the sum.
Since all values of are sorted in descending order, it is possible to first find the maximum position such that . For all subsequent positions, the contribution to the desired sum will be zero, so these positions can be simply forgotten. For the found prefix of positions , the idea from point 6 can be used, but it is necessary to learn how to quickly calculate the number of numbers greater than a given one in the prefix. This can be done using the technique of "partial cascading". This technique allows you to respond to necessary requests in the same time and requires time and memory for construction. With its help, the problem can be solved in time .
Solution: [link to the solution]
Block 11
To speed up the previous solution, it is sufficient to use the idea described in point 5. The complexity of this solution is .
Solution: [link to the solution]
It is also worth noting that there is another, simpler, solution that was found during the testing of the problem by Yevhen Sobolev. The main idea is that a simple binary search for the answer can be used, but to check that the answer is valid, it is necessary to check not at the point , but in the window of size on the interval . To quickly check the criterion described in point 2, it is possible to first find the value of the sum at in time. To recalculate the value of the sum when increasing by 1, it can be noted that each term of the sum can increase at most once by . Indeed, when increasing by 1, the numerator of the fraction increases by , and the denominator equals . Since the inequality holds, then . Therefore, when increasing by , the value of the fraction can increase at most for only times, so the value of the fraction can increase at most by . The time at which this value will increase can be found using the formula, and at the appropriate time, the current value of the sum should be increased by . Therefore, it is possible to solve the problem in time .
Solution: [link to the solution]
Notes and Interesting Facts:
When implementing the solution, it is necessary to be very careful with multiplication operations to avoid accidentally exceeding the limits of the 64-bit data type. The most dangerous place where the 64-bit type can overflow is the multiplication of by in the check function (for example, if setting the limits of the binary search to ). However, it can be noted that since , then . Since the answer does not exceed (), then . This means that the right limit of the binary search can be set to the value , and there is no need to worry about overflow anymore.
This problem was initially proposed for position "A" with an incorrect editorial solution described in point 2 (a simple binary search for the answer). After finding the correct solution, the complexity of the problem sharply increased, and the problem was moved to the last position.