AtCoder/ABC138
https://atcoder.jp/contests/abc138
A - Red or Not
puts (gets.to_i >= 3200) ? gets.chomp : "red"
B - Resistors in Parallel
gets as = gets.split.map(&:to_i) puts Rational(1, as.map {|i| Rational(1, i)}.inject(:+)).to_f
C - Alchemist
gets as = gets.split.map(&:to_i).sort def calc(ary) return ary.first if ary.size == 1 a = Rational(ary[0] + ary[1], 2) calc([a] + ary[2..-1]) end puts calc(as).to_f
いや、これわからんよ。思いつかないし。
D - Ki
n, q = gets.split.map(&:to_i) #頂点 a と b を結ぶ tree = Array.new(n + 1) {[]} (n - 1).times do a, b = gets.split.map(&:to_i) tree[a] << b tree[b] << a end #頂点 p は根で、部分木のすべての頂点に足されるのは x total = Array.new(n + 1, 0) q.times do p, x = gets.split.map(&:to_i) total[p] += x end #実際に足す操作 visited = Array.new(n + 1) queue = [1] until queue.empty? c = queue.shift visited[c] = true tree[c].each do |q| next if visited[q] total[q] += total[c] queue << q end end puts total[1..n].join(" ")
RE はスタックオーバーフローになっていたみたいだ。