ALDS1_3_B: Queue

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B
Arrayを使えばシングルスレッドでstackは実現できるのにqueueはなぜかマルチスレッドしかない?
まだRuby4日目だからよくわからない、、、

# your code goes here
require 'thread' 
n,q=STDIN.gets.split(" ")
n=n.to_i
q=q.to_i

qu=Queue.new
n.times do |i|
n1,t1=STDIN.gets.split(" ")
qu<<[n1,t1.to_i]
end
th1=Thread.start do
time=0
while !qu.empty?
	a=qu.pop
	if a[1]<=q
		time+=a[1]
		print a[0]<<" "
		puts time
	else
		time+=q
		t2=a[1]-q
		qu.push([a[0],t2])
	end
end
end
th1.join
最終更新:2016年05月29日 12:39