Editorial for COCI '09 Contest 5 #5 Program
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us start with the brute force solution. We simulate the calls to the something
function. Using appropriate structure (for example tournament tree) we answer the sequence sum question in time.
Now for the first improvement. If there are multiple times that number appears in the input ( times for example): there is no need to call the something
function times. Instead, we can call a modified version of the function once, and have her increment the fields by . This eliminates all duplicates in the input sequence.
What is the complexity of this solution? The worst case for the brute force algorithm is ones leading to complexity. But our new modified function solves such a case in one pass. Obviously, we can have only one number in the worst case. The next worst thing is to enter number . Then . And so on. So the worst case is now . Our modified function will perform steps. But it can be easily shown that this is at most . This is solvable in the given time limit.
Comments