Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5
- 1 7 5 2 3
- fin = File.open("input.txt", "r")
- fout = File.open("output.txt", "w")
- n = fin.readline.to_i
- heap_min = Heap.new(:min)
- heap_max = Heap.new(:max)
- for i in 1..n
- a = fin.read.to_i #code here <--
- heap_max.push(a)
- if heap_max.size > heap_min.size
- tmp = heap_max.top
- heap_max.pop
- heap_min.push(tmp)
- end
- if heap_min.size > heap_max.size
- tmp = heap_min.top
- heap_min.pop
- heap_max.push(tmp)
- end
- if heap_max.size == heap_min.size
- heap_max.top > heap_min.top ? median = heap_min.top : median = heap_max.top
- else
- median = heap_max.top
- end
- fout.print(median, " ")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement