Editorial for COCI '10 Contest 1 #2 Profesor


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.

Observe that checking every possible interval and every possible grade is too slow for a sufficiently large N. Such a solution has a complexity of \mathcal O(N^3) and is worth 70\% of points.

To obtain a complete score, we need another approach.

If we assume the grade which the professor will award the students, we can, in a single pass, determine the longest continuous subsequence of desks such that each desk contains at least one student deserving the assumed grade. This can be implemented using a counter storing the current number of continuous desks, and updating it for each desk.

Finally, the solution consists of iterating the described algorithm for each grade from 1 through 5 and taking the maximum of the individual results.

Also observe that the problem can be solved with complexity \mathcal O(N) even if grades are from the interval 1 through N. Solving this problem is left as an exercise for the reader.


Comments

There are no comments at the moment.