/** * Observations: * - Moving low number to the left takes forever * - Moving large numbers to the right goes really fast * tactic: Calculate the distance the number must travel for numbers traveling left. In a single iteration this is ceil(distance/(k-1)). * pitfall: Doublets should not be tried to move to the same position, but to the earliest unused one. */ fun main() { val (_, k) = readLine()!!.split(" ").map(String::toInt) val numbers = readLine()!!.split(" ").map(String::toLong).toList() val target = numbers.sorted().toMutableList() val max = numbers.withIndex().maxOf { (index, it) -> index - target.indexOf(it).also { target[it] = -1 } } println(max ceilDivision k - 1) } infix fun Int.ceilDivision(b: Int) = this / b + when (this % b) { 0 -> 0 else -> 1 } // some testing notes, can be removed /* example more right(input 2) -6 0 0 3 -3 2 1 3 60 08 27 07 68 41 53 44 08 27 60 07 27 60 27 60 68 41 60 68 53 60 68 44 60 68 08 07 27 41 53 44 60 68 07 08 27 08 27 41 27 41 53 41 44 53 44 53 60 53 60 68 07 08 27 41 44 53 60 68 */ /* example move right(input 3) -3 0-3 2 1 5 : Distance 3 2 4 2 3 1 2 3 4 2 3 4 3 3 4 1 3 4 2 2 3 1 3 4 2 2 3 1 2 3 2 3 3 3 3 4 2 1 2 3 3 4 1 2 2 2 2 3 2 3 3 3 3 4 1 2 2 3 3 4 */ /* Reverse order case (4 iteraties) 5 2 5 4 3 2 1 -4-2 0 2 4 5 4 3 2 1 4 5 3 5 2 5 1 5 4 3 2 1 5 3 4 2 4 1 4 4 5 3 2 1 4 5 2 3 1 3 2 1 3 4 5 1 2 1 2 3 4 5 */ /* Weird case for with doubles, make sure not target the first occurence of the sorted list. 5 2 5 5 4 5 5 5 5 4 5 5 4 5 5 5 4 5 5 5 5 Answer should be 2 not 4(last 5 was moved to index 1) */