AOJ
0272: The Lonely Girl's Lie むずかしそうだったが、よく考えたら解けてうれしい。 while true n = gets.to_i break if n.zero? as = gets.split.map(&:to_i).sort bs = gets.split.map(&:to_i).sort try = ->{ (1...n).each do |k| l = (k / 2r).ceil min1…
0256: Points for a Perfect Scorer puts Array.new(10) { gets.to_i }.sum 0257: Railway Ticket b = gets.split.join.to_i(2) table = {4=>false, 2=>false, 6=>true, 1=>true, 0=>false} puts table[b] ? "Open" : "Close" 0258: Kitchen Garden 簡単そう…
0230 Ninja Climbing WA。いいと思うんだけれどなあ。 KABE = 0 HASHIGO = 1 SUBERU = 2 loop do n = gets.to_i break if n.zero? bils = Array.new(2) { gets.split.map(&:to_i) } bil_ck_init = Array.new(n) #trueだと到達済み #aがいま居るビル、bが反対…
0220 Binary Digit A Doctor Loved 小数の2進表現かあ。勉強になるなあ。なお、10進表現で循環小数でなくとも、2進表現で循環小数になる場合がある。 def calc(r, str = "") return str if str.length > 4 a = r * 2 b = a.to_i str += b.to_s c = a - b if …
0210 The Squares むずかしかった。何とか自力で出来た。 Table = %W(E N W S) Man = Struct.new(:x, :y, :dir, :next_x, :next_y) do def next dx, dy = [[1, 0], [0, -1], [-1, 0], [0, 1]][self.dir] self.next_x, self.next_y = self.x + dx, self.y + d…
0200 Traveling Alone: One-way Ticket of Youth until (given = gets.split.map(&:to_i)) == [0, 0] n, m = given edge = Array.new(m) {[]} h = Array.new(m) {[]} n.times do a, b, cost, time = gets.split.map(&:to_i) a -= 1 b -= 1 edge[a][b] = edge…
AIZU ONLINE JUDGE: Programming Challenge 0190 Eleven Puzzle 素直に幅優先探索を実行して死んだので、ここを参考に解いた。「両側探索」というらしい。 start = "fff0fff" "ff123ff" "f45678f" "ff9abff" "fff0fff" result1 = {} result1[start] = 0 stac…
AIZU ONLINE JUDGE: Programming Challenge 0180 Demolition of Bridges until (given = $<.gets.split.map(&:to_i)) == [0, 0] n, m = given bridges = m.times.map {$<.gets.split.map(&:to_i)}.sort_by(&:last) city = Array.new(n, false) city[0] = tru…
AIZU ONLINE JUDGE: Programming Challenge ALDS1_9_A Complete Binary Tree #完全二分木 $<.gets given = [nil] + $<.gets.split.map(&:to_i) a = nil given.drop(1).each_with_index do |key, i| j = i + 1 str = "node #{j}: key = #{key}, " str += "par…
AIZU ONLINE JUDGE: Programming Challenge 0170 Lunch until (n = $<.gets.to_i).zero? data = n.times.map {$<.gets.split}.map {|a, *b| [a] + b.map(&:to_i)} selected = [] all = [*0...n] solve = ->(left, order) { return if data[order.last][2] < …
AIZU ONLINE JUDGE: Programming Challenge 0160 Delivery Fee table = [[60, 2, 600], [80, 5, 800], [100, 10, 1000], [120, 15, 1200], [140, 20, 1400], [160, 25, 1600]] until (n = $<.gets.to_i).zero? result = n.times.map {$<.gets.split.map(&:to…
AIZU ONLINE JUDGE: Programming Challenge 0150 Twin Prime N = 10007 sieve = [*0..N] 2.upto(Math.sqrt(N).to_i) do |i| next if sieve[i].zero? 2.upto(N / i) {|j| sieve[i * j] = 0} end sieve = sieve[2..-1].reject {|x| x.zero?} until (n = $<.get…
AIZU ONLINE JUDGE: Programming Challenge 0140 Bus Line line = [*0..9] + [5, 4, 3, 2, 1] line += line $<.gets.to_i.times do start, goal = $<.gets.split.map(&:to_i) str = if 1 <= start and start <= 5 and start > goal [*goal..start].reverse.j…
AIZU ONLINE JUDGE: Programming Challenge 0130 Train $<.gets.to_i.times do given = $<.gets.chomp.split(/(->|<-)/) train = [given.shift] while (dir = given.shift) car = given.shift next if train.include?(car) (dir == "->") ? train.push(car) …
AIZU ONLINE JUDGE: Programming Challenge 0120 Patisserie @memo = {} def length(circles) return @memo[circles] if @memo[circles] l = case (s = circles.size) when 0 then 0 when 1 then 0 when 2 r1, r2 = circles.first, circles.last Math.sqrt((…
AIZU ONLINE JUDGE: Programming Challenge 0110 Alphametic $<.readlines.map(&:chomp).map {|l| l.split(/\+|=/)}.each do |given| catch(:jump) do 10.times do |i| d = given.map {|a| a.gsub("X", i.to_s)} next unless d.select {|s| s.length >= 2 an…
AIZU ONLINE JUDGE: Programming Challenge 0100 Sale Result 問題が曖昧。同じ社員が二度出てくるかどうかはっきりしない。 Border = 1_000_000 until (n = $<.gets.to_i).zero? data = Hash.new(0) entry = [] n.times do e, p, q = $<.gets.split.map(&:t…
AIZU ONLINE JUDGE: Programming Challenge 0091 Blur L = 10 table = [[[0, 0], [0, 1], [-1, 1], [1, 1], [0, 2], [-1, 2], [-2, 2], [1, 2], [2, 2], [0, 3], [-1, 3], [1, 3], [0, 4]], [[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1], [0, 2], [1, 2…
AIZU ONLINE JUDGE: Programming Challenge 0081 A Symmetric Point require 'matrix' $<.readlines.map {|l| l.split(",").map(&:to_f)}.each do |x1, y1, x2, y2, xq, yq| p1 = Vector[x1, y1] n = Vector[y2 - y1, x1 - x2].normalize q = Vector[xq, yq]…
AIZU ONLINE JUDGE: Programming Challenge 0071 Bombs Chain $<.gets.to_i.times do |co| $<.gets field = Array.new(8) {$<.gets.chomp.chars.map(&:to_i)} xi, yi = $<.gets.to_i, $<.gets.to_i blast = ->(x, y) { field[y][x] = 2 3.times do |i| [[1, …
AIZU ONLINE JUDGE: Programming Challenge 0061 Rank Checker data = [] until (st = $<.gets.chomp) == "0,0" data << st.split(",").map(&:to_i) end data.sort! {|a, b| b[1] <=> a[1]} x = data.first.last h = {} l = 1 data.each do |d| l += 1 unles…
AIZU ONLINE JUDGE: Programming Challenge 0051 Differential II $<.readlines.drop(1).map {|a| a.chomp.chars.map(&:to_i).sort}.each do |ary| puts ary.reverse.join.to_i - ary.join.to_i end 0052 Factorial II until (n = $<.gets.to_i).zero? five …
AIZU ONLINE JUDGE: Programming Challenge 0041 Expression def solve(ary) if ary.size <= 1 return ary[0] if eval(ary[0]) == 10 else idxs = [*0...ary.size] idxs.combination(2) do |i, j| a, b = ary[i], ary[j] nxt = (idxs - [i, j]).map{|x| ary[…
AIZU ONLINE JUDGE: Programming Challenge 0031 Weight def measure(object, weight, result = []) return result.join(" ") if weight.zero? result.unshift(weight) if (object / weight).nonzero? measure(object % weight, weight / 2, result) end $<.…
AIZU ONLINE JUDGE: Programming Challenge 0021 Parallelism $<.readlines.drop(1).map {|a| a.split.map(&:to_r)}.each do |x1, y1, x2, y2, x3, y3, x4, y4| puts((x2 - x1) * (y4 - y3) == (x4 - x3) * (y2 - y1) ? "YES" : "NO") end 単純な問題なのに…
AIZU ONLINE JUDGE: Programming Challenge 0011 Drawing Lots given = $<.readlines lines = [*0..given.first.to_i] given.drop(2).map {|x| x.split(",").map(&:to_i)}.each do |a, b| lines[a], lines[b] = lines[b], lines[a] end puts lines.drop(1) 0…
AIZU ONLINE JUDGE: Programming Challenge 0000 QQ 9.times do |i| 9.times {|j| puts "#{x = i + 1}x#{y = j + 1}=#{x * y}"} end 0001 List of Top 3 Hills puts $<.readlines.map(&:to_i).sort {|a, b| b <=> a}.take(3) 0002 Digit Number $<.readlines…
AIZU ONLINE JUDGE: Programming Challenge ALDS1_1_A #挿入ソート gets ar = gets.split.map(&:to_i) n = ar.size putout = ->{puts ar.join(" ")} putout.() 1.upto(n - 1) do |i| v = ar[i] j = i - 1 while j >= 0 and ar[j] > v ar[j + 1] = ar[j] j -=…
AIZU ONLINE JUDGE: Programming ChallengeITP1_1_A puts "Hello World" ITP1_1_B x = gets.to_i puts x ** 3 ITP1_1_C a, b = gets.split.map(&:to_i) puts "#{a * b} #{2 * (a + b)}" ITP1_1_D n = gets.to_i puts "#{n / 3600}:#{(n % 3600) / 60}:#{n % …