Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*
- import kotlin.math.abs
- fun main(args: Array<String>) {
- val treeSet: TreeSet<Int> = TreeSet()
- val scanner = Scanner(System.`in`)
- val inpSize = scanner.nextInt()
- for (i in 0 until inpSize) {
- when (scanner.next()) {
- "+1" -> treeSet.add(scanner.nextInt())
- "-1" -> treeSet.remove(scanner.nextInt())
- "0" -> {
- var inp = scanner.nextInt()
- println(nth(treeSet, abs(inp), if (inp > 0) ::higherZero else ::lowerZero))
- }
- }
- }
- }
- fun nth(treeSet: TreeSet<Int>, n: Int, function: (TreeSet<Int>) -> Int): Int {
- val list: LinkedList<Int> = LinkedList()
- for (i in 1 until n) {
- list.add(function(treeSet))
- treeSet.remove(list.last)
- }
- val max = treeSet.floor(Int.MAX_VALUE)!!
- treeSet.addAll(list)
- return max
- }
- fun lowerZero(treeSet: TreeSet<Int>): Int{
- return treeSet.ceiling(Int.MIN_VALUE)!!
- }
- fun higherZero(treeSet: TreeSet<Int>): Int{
- return treeSet.floor(Int.MAX_VALUE)!!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement