AtCoder(AtCoder Beginners Selection)
AtCoder Beginners Selection - AtCoder
PracticeA - はじめてのあっとこーだー(Welcome to AtCoder)
a = gets.to_i b, c = gets.split.map(&:to_i) s = gets print "#{a + b + c} #{s}"
ABC086A - Product
a, b = gets.split.map(&:to_i) puts (a * b).odd? ? "Odd" : "Even"
ABC081A - Placing Marbles
puts gets.chomp.count("1")
ABC081B - Shift only
def calc(ary, co) return co unless ary.all?(&:even?) calc(ary.map {|i| i / 2}, co + 1) end gets given = gets.split.map(&:to_i) puts calc(given, 0)
ABC087B - Coins
table = [500, 100, 50] coins = 3.times.map {gets.to_i} x = gets.to_i memo = Array.new(4) {[]} try = ->(i, yen) { return memo[i][yen] if memo[i][yen] return 1 if yen.zero? return 0 if i >= 3 co = 0 (0..coins[i]).each do |n| yen1 = yen - table[i] * n break if yen1 < 0 co += try.(i + 1, yen1) end memo[i][yen] = co } puts try.(0, x)
ABC083B - Some Sums
n, a, b = gets.split.map(&:to_i) puts (1..n).select {|i| i.to_s.bytes.map {|b| b - 48}.inject(&:+).between?(a, b)} .inject(&:+)
ABC088B - Card Game for Two
n = io.gets.to_i cards = io.gets.split.map(&:to_i).sort diff = 0 (0...n).each do |i| if i.even? diff += cards[n - 1 - i] else diff -= cards[n - 1 - i] end end puts diff
ABC085B - Kagami Mochi
n = gets.to_i puts n.times.map {gets.to_i}.uniq.size
ABC085C - Otoshidama
n, y = gets.split.map(&:to_i) limit1 = [y / 10000, n].min (0..limit1).each do |b1| yen1 = y - 10000 * b1 limit2 = [yen1 / 5000, n - b1].min (0..limit2).each do |b2| b3 = n - b1 - b2 if yen1 == 5000 * b2 + 1000 * b3 puts "#{b1} #{b2} #{b3}" exit end end end puts "-1 -1 -1"
これで 0.216秒。
よりよい方法がよく考えたらわかった。
n, y = gets.split.map(&:to_i) limit1 = [y / 10000, n].min (0..limit1).each do |b1| x = y / 1000 - n - 9 * b1 if x >= 0 and (x % 4).zero? b2 = x / 4 b3 = n - b1 - b2 if b3 >= 0 and 10000 * b1 + 5000 * b2 + 1000 * b3 == y puts "#{b1} #{b2} #{b3}" exit end end end puts "-1 -1 -1"
これで 0.009秒。
ABC049C - 白昼夢 / Daydream
table = %w(dream dreamer erase eraser).map(&:reverse) s = gets.chomp.reverse result = "YES" until s.length.zero? catch :jump do table.each do |word| n = word.length if s[0, n] == word s = s[n..-1] throw :jump end end result = "NO" s = "" end end puts result
ABC086C - Traveling
n = gets.to_i plans = n.times.map {gets.split.map(&:to_i)} f = plans.all? do |t, x, y| d = x.abs + y.abs if t < d false else t.odd? ? d.odd? : d.even? end end puts f ? "Yes" : "No"