Editorial for COCI '11 Contest 2 #6 Raspored
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's say that we are given some permutation (order in which pizzas are made) , and we wish to calculate the tip we'll get:
We can now conclude that order with regard to deadlines is not important, and that order with regard to durations must be increasing, so that pizzas that are baked longer are multiplied by a smaller coefficient.
Brute force solution that sorts the pizzas after each update and calculates the above sum has complexity or and is worth 50 points.
The constraint given to was sort of a hint that can lead to a simple solution: in some array we can use to store the number of pizzas having baking time . For under , we calculate every query by traversing the array. This approach was worth 80 points.
We can use some data structure like segmented array (BIT) for implementing the array, and additional sequence that keeps the sums (). To remove duration for some pizza , we must increase our solution by:
Now we decrease by and by . In order to insert into our structure, we do the same thing, but with inversed signs and order (change and first and then decrease the solution). sums are trivial to calculate and don't require any data structures.
We can answer each query in , and total complexity is which is good enough for obtaining maximum points for this task.
There is an alternative solution that doesn't depend on . Idea is to maintain the sorted sequence of all the baking durations, along with the sums and corresponding coefficients. This can be achieved by using a balanced tree (online solution), and by using a segmented array built on top of the sorted sequence of all the durations (offline solution).
The complexity of this approach is .
Comments