Editorial for COCI '19 Contest 3 #2 Grudanje
Submitting an official solution before solving the problem yourself is a bannable offence.
In order to score , it was enough to simply simulate the process described in the problem statement.
The time complexity of that solution is .
In order to score an additional , you could use prefix sums to speed up the solution above. Suppose that for every letter you built an array in which an element at position stores the number of occurrences of that letter from the beginning of the string to the letter (inclusive). For example, for a word abcaab
and a letter a
, the array would be . That array is useful because now we can determine in what is the number of occurrences of a certain letter in an interval from to the letter in our original word. The formula for that is .
The idea is now to precompute this array for every letter, thus obtaining an algorithm with complexity , where denotes the size of our alphabet.
In order to score all points, you could observe the following: if a word is perfect after snowball, it will also be perfect after every other snowball thrown after the 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:
Comments