Advertisement
vkazar

Untitled

Nov 12th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.36 KB | None | 0 0
  1. package sprint5.fin.A
  2.  
  3. import java.io.BufferedReader
  4. import java.io.InputStreamReader
  5. import java.util.*
  6.  
  7. data class Student(val name: String, val passedCount: Int, val penalty: Int) : Comparable<Student> {
  8.     override fun compareTo(other: Student): Int =
  9.         compareBy<Student> { it.passedCount }
  10.             .thenByDescending { it.penalty }
  11.             .thenByDescending { it.name }
  12.             .compare(this, other)
  13. }
  14.  
  15. fun main() {
  16.     val buffer = BufferedReader(InputStreamReader(System.`in`))
  17.     List(buffer.readLine().toInt()) {
  18.         val tokenizer = StringTokenizer(buffer.readLine())
  19.         Student(tokenizer.nextToken(), tokenizer.nextToken().toInt(), tokenizer.nextToken().toInt())
  20.     }
  21.         .heapSort()
  22.         .joinToString("\n") { it.name }
  23.         .let(::println)
  24. }
  25.  
  26. fun <T : Comparable<T>> List<T>.heapSort(): List<T> {
  27.     return MyHeap<T>()
  28.         .also { heap ->
  29.             this.forEach { heap.put(it) }
  30.         }
  31.         .let { heap ->
  32.             buildList {
  33.                 while (!heap.isEmpty)
  34.                     add(heap.pop())
  35.             }
  36.         }
  37. }
  38.  
  39. class MyHeap<T : Comparable<T>> {
  40.     private val heap: MutableList<T?> = mutableListOf(null)
  41.     val isEmpty
  42.         get() = heap.size == 1
  43.  
  44.     fun put(element: T) {
  45.         heap.add(element)
  46.         siftUp(heap.lastIndex)
  47.     }
  48.  
  49.     fun pop(): T {
  50.         heap.swap(1, heap.lastIndex)
  51.         return heap.removeLast()
  52.             .also { siftDown(1) }!!
  53.     }
  54.  
  55.  
  56.     private fun siftDown(idx: Int): Int {
  57.         val left = (idx * 2).takeIf { it < heap.size }
  58.         val right = left?.let { it + 1 }?.takeIf { it < heap.size }
  59.         val children = listOfNotNull(left, right)
  60.  
  61.         return if (children.isEmpty()) idx
  62.         else children
  63.             .filter { heap[it]!! > heap[idx]!! }
  64.             .maxByOrNull { heap[it]!! }
  65.             ?.let {
  66.                 heap.swap(it, idx)
  67.                 siftDown(it)
  68.             }
  69.             ?: idx
  70.     }
  71.  
  72.     private fun siftUp(idx: Int): Int {
  73.         if (idx == 1) return idx
  74.         val parent = idx / 2
  75.         if (heap[parent]!! > heap[idx]!!) return idx
  76.         heap.swap(parent, idx)
  77.         return siftUp(parent)
  78.     }
  79.  
  80.     private fun MutableList<T?>.swap(left: Int, right: Int) {
  81.         val tmp = this[left]
  82.         this[left] = this[right]
  83.         this[right] = tmp
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement