Editorial for COCI '10 Contest 3 #4 Znanstvenik


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.

It is important to notice that if it is possible to delete K rows from the top of the matrix leaving no two equal columns, the condition will also be satisfied if only K-1 rows are deleted. Hence the maximum K can be found using binary search.

Now we need an efficient method to check, for a given K, whether it is possible to delete the first K rows leaving no two equal columns. If we read the columns as strings from the bottom up, a good idea is to sort them lexicographically. That way, any two equal columns will be next to each other, so we only need to check adjacent columns for equality up to length K.

The described solution can be implemented with complexity \mathcal O(N^2 \log N), where N = \max(R, C).

An alternative solution could use hashing of individual columns, reducing column comparison to simple integer comparison (reducing complexity in the process). The details of this solution are left as an exercise for the reader.


Comments

There are no comments at the moment.