Editorial for DMOPC '18 Contest 1 P2 - Sorting


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.

Author: Kirito

For 30% of points, we have 1K2. If K=1, then the list can be sorted for sure. If K=2, then we can separate the list into elements with even and odd indices, sort those lists, and check if alternating between the two lists yields a sorted list.

Time Complexity: O(NlogN)

For the remaining 70% of points, we can check if AiimodK, for all i, 1iN. This is because in the sorted list, Ai=i, and the ith element can only be swapped with elements that are equivalent modulo K.

Time Complexity: O(N)


Comments

There are no comments at the moment.