Editorial for COCI '09 Contest 5 #5 Program


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
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 \mathcal O(1) time.

Now for the first improvement. If there are multiple times that number Y appears in the input (X times for example): there is no need to call the something function X times. Instead, we can call a modified version of the function once, and have her increment the fields by X. This eliminates all duplicates in the input sequence.

What is the complexity of this solution? The worst case for the brute force algorithm is K ones leading to \mathcal O(NK) complexity. But our new modified function solves such a case in one pass. Obviously, we can have only one number 1 in the worst case. The next worst thing is to enter number 2. Then 3. And so on. So the worst case is now \{1, 2, \dots, K\}. Our modified function will perform \frac N 1 + \frac N 2 + \dots + \frac N K steps. But it can be easily shown that this is at most 20N. This is solvable in the given time limit.


Comments

There are no comments at the moment.