Advertisement
Guest User

Untitled

a guest
Apr 15th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.05 KB | None | 0 0
  1. import java.util.*
  2. import kotlin.math.abs
  3.  
  4. fun main(args: Array<String>) {
  5.     val treeSet: TreeSet<Int> = TreeSet()
  6.     val scanner = Scanner(System.`in`)
  7.     val inpSize = scanner.nextInt()
  8.     for (i in 0 until inpSize) {
  9.         when (scanner.next()) {
  10.             "+1" -> treeSet.add(scanner.nextInt())
  11.             "-1" -> treeSet.remove(scanner.nextInt())
  12.             "0" -> {
  13.                 var inp = scanner.nextInt()
  14.                 println(nth(treeSet, abs(inp), if (inp > 0) ::higherZero else ::lowerZero))
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. fun nth(treeSet: TreeSet<Int>, n: Int, function: (TreeSet<Int>) -> Int): Int {
  21.     val list: LinkedList<Int> = LinkedList()
  22.     for (i in 1 until n) {
  23.         list.add(function(treeSet))
  24.         treeSet.remove(list.last)
  25.     }
  26.     val max = treeSet.floor(Int.MAX_VALUE)!!
  27.     treeSet.addAll(list)
  28.     return max
  29. }
  30.  
  31. fun lowerZero(treeSet: TreeSet<Int>): Int{
  32.     return treeSet.ceiling(Int.MIN_VALUE)!!
  33. }
  34.  
  35. fun higherZero(treeSet: TreeSet<Int>): Int{
  36.     return treeSet.floor(Int.MAX_VALUE)!!
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement