Editorial for COCI '13 Contest 4 #5 Čokolade
Submitting an official solution before solving the problem yourself is a bannable offence.
To begin with, we will explain the solution of an easier version of this task, which appeared in the original Croatian edition of COCI, called HONI. In the HONI version, the candy is from the interval , whereas in the COCI version it is from the interval . Also, in HONI would not exceed , whereas in COCI it would not exceed .
We will interpret the task in the following way: for an array of numbers of length , for each from to , we need to determine the minimal number with which we need to divide every array item so that we are left with a group of exactly equal numbers in the array or we need to determine that such number doesn't exist. If we mark the greatest number in the array with , it is clear that all the solutions belong in the interval .
Our first solution is now formed, its complexity being . For each from the interval , we can determine how many numbers give a certain quotient when performing division with with the help of a counter in an array, which we update accordingly.
For a solution which performs for of points, we need to come up with a faster way to calculate how many numbers give a certain quotient when performing division with the number .
The following can be noticed:
- the numbers which give the quotient when performing (integer) division with are from the interval
- the numbers which give the quotient when performing (integer) division with are from the interval
- in general, the numbers which give the quotient when performing (integer) division with are from the interval
If we can build a structure which can quickly give an answer to the query How many numbers in the array are from the interval then we can determine how many numbers give the quotient when performing division with . Given the fact that the array numbers are not greater than , the structure can be a simple array where stands for the number of elements in the original array smaller than . Now we can calculate the number of numbers in the original array from the interval with the formula .
The greatest quotient which can happen for a certain is . This is why we need to make queries to determine how many numbers there are which give the quotient when performing division with . Because we have to test all numbers from to for a certain , we have queries in total. Therefore, the complexity of this algorithm is .
The COCI version of this task had different limitations: , . This version required a different approach.
For each number in the array, we can observe the following array: , where marks the floor of value . We can notice that this array is going to consist of blocks of consecutive equal values. We will call number important if is different from .
Now we can use an algorithm similar to the initial solution of complexity , except now we don't need to test every from to . Instead, it is sufficient to check only the important numbers for each element we have in the array because the not important number will have all quotients equal to those of some important number.
Now the complexity is . It can be shown that for a number , there exist different important numbers at most, so the total complexity is .
Comments