Editorial for COCI '19 Contest 3 #2 Grudanje


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.

In order to score 20\%, it was enough to simply simulate the process described in the problem statement.

The time complexity of that solution is \mathcal O(NQN) = \mathcal O(QN^2).

In order to score an additional 20\%, you could use prefix sums to speed up the solution above. Suppose that for every letter you built an array pref in which an element at position i stores the number of occurrences of that letter from the beginning of the string to the i^\text{th} letter (inclusive). For example, for a word abcaab and a letter a, the array pref would be [1, 1, 1, 2, 3, 3]. That array is useful because now we can determine in \mathcal O(1) what is the number of occurrences of a certain letter in an interval from L^\text{th} to the R^\text{th} letter in our original word. The formula for that is pref[R]-pref[L-1].

The idea is now to precompute this array for every letter, thus obtaining an algorithm with complexity \mathcal O(N(N\Sigma \times Q\Sigma)) = \mathcal O((N^2+Q)\Sigma), where \Sigma denotes the size of our alphabet.

In order to score all points, you could observe the following: if a word is perfect after i^\text{th} snowball, it will also be perfect after every other snowball thrown after the i^\text{th} one. This property allows us to use binary search. The check inside binary search is done similarly to the algorithm above (using prefix sums).

Time complexity: \mathcal O((N\Sigma + Q\Sigma) \log N) = \mathcal O((N \log N + Q)\Sigma)


Comments

There are no comments at the moment.