Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun <T : Any> Array<T>.pyramidSort(comparator: Comparator<T>) {
- for (i in lastIndex downTo 1) {
- heapForRange(0, i, comparator)
- swap(0, i)
- }
- }
- private fun <T : Any> Array<T>.heapForRange(start: Int, end: Int, comparator: Comparator<T>) {
- for (i in end downTo start) {
- siftUp(i, comparator)
- }
- }
- private fun <T : Any> Array<T>.siftUp(index: Int, comparator: Comparator<T>) {
- val parentIndex = if (index == 0) 0 else (index - 1) / 2
- if (comparator.compare(this[parentIndex], this[index]) < 0) {
- swap(parentIndex, index)
- siftUp(parentIndex, comparator)
- }
- }
- private fun <T: Any> Array<T>.swap(left: Int, right: Int) {
- val temp = this[right]
- this[right] = this[left]
- this[left] = temp
- }
- /**
- * Представление стажёра.
- */
- data class Intern (
- val name: String,
- val taskCount: Int,
- val penalty: Int
- )
- fun main() {
- val reader = System.`in`.bufferedReader()
- val writer = System.out.bufferedWriter()
- val count = reader.readLine().toInt()
- val comparator = compareByDescending<Intern> { it.taskCount }
- .thenBy { it.penalty }
- .thenBy { it.name }
- val array = Array(count) {
- val (name, taskCount, penalty) = reader.readLine().split(" ")
- Intern(name, taskCount.toInt(), penalty.toInt())
- }
- array.pyramidSort(comparator)
- with(writer) {
- array.forEach {
- println(it.name)
- }
- flush()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement