Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. 5
  2. 1 7 5 2 3
  3.  
  4. fin = File.open("input.txt", "r")
  5. fout = File.open("output.txt", "w")
  6. n = fin.readline.to_i
  7.  
  8. heap_min = Heap.new(:min)
  9. heap_max = Heap.new(:max)
  10.  
  11. for i in 1..n
  12.  
  13. a = fin.read.to_i #code here <--
  14.  
  15. heap_max.push(a)
  16. if heap_max.size > heap_min.size
  17. tmp = heap_max.top
  18. heap_max.pop
  19. heap_min.push(tmp)
  20. end
  21. if heap_min.size > heap_max.size
  22. tmp = heap_min.top
  23. heap_min.pop
  24. heap_max.push(tmp)
  25. end
  26. if heap_max.size == heap_min.size
  27. heap_max.top > heap_min.top ? median = heap_min.top : median = heap_max.top
  28. else
  29. median = heap_max.top
  30. end
  31. fout.print(median, " ")
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement