Editorial for GFSSOC '15 Fall J5 - Nightmare-a-thon
Submitting an official solution before solving the problem yourself is a bannable offence.
This problem was quite challenging for the junior level (thank Timothy Li), but the solution is super cool! Because of the high bounds on , we needed or time complexity queries to pass. The easiest solution is in time. Let's break this problem down into the two parts of its queries. The first part asks for the highest rated episode not within the range . This is very difficult because it is hard to do anything from the middle of an array. How do we change this problem to make it easier for us? Well, the query is basically asking us the highest rated episode from episode to episode , or from episode to episode , right? Now, to find the highest from episode to episode , make a maximum array, sweep through the episodes left to right, keeping track of the highest rated episode we've found so far in your max array. Similarly, for episodes to , keep a second maximum array, sweep through the episodes right to left, keeping track of the highest ep. we've found so far. An example of this:
Episodes: 1 3 2 4 3 5 2
Maxleft: 1 3 3 4 4 5 5
Maxright: 5 5 5 5 5 5 2
Now, to find the highest rating not within episodes and , simply take the max between maxleft[a-1]
and maxright[b+1]
.
The second part of the query can be solved in conjunction with the first. Create 2 more occurrence/frequency arrays. Whenever you update your maximum array to a higher rating, the occurrences of high should be set to 1 (since this new high is the only one you've found!). Whenever you find an episode that has equal rating to your previous high, the occurrence at that index should equal the occurrence at the previous index + 1. Otherwise, the occurrence at the current index should equal the occurrence at the previous index. An example iterating from the left side:
Episodes: 1 3 2 4 4 5 2
Maxleft: 1 3 3 4 4 5 5
Freqleft: 1 1 1 1 2 1 1
Now, to answer the second part of the query, we need to consider a few cases. If the left side max is greater than the right side max, we need to take the occurrences of the left side max (aka Freqleft[a-1]
). Similarly if right side max > left side max, we take Freqright[b+1]
. However, if left side max == right side max, we need to find the combined occurrences of this number in both the left side and right side, thus we take Freqleft[a-1] + Freqright[b+1]
.
Comments