PHP と Ruby による AtCoder Beginner Contest 121
PHP の練習を兼ねて Atcorder Beginner を最近解き始めたのでログを残す。 DP が苦手でいつも C 問題が解けるか、解けないかを彷徨うレベルですが。。
今回は三完ですた。D問題はビット演算のメモ化とかするのかなあ。
A (勉強ちうのPHP)
<?php fscanf(STDIN,'%d %d',$H,$W); fscanf(STDIN,'%d %d',$h,$w); $sum = $H * $W; $sum = $sum - $h * $W; $left_row = $H - $h; $sum = $sum - $left_row * $w; echo $sum;
B (勉強ちうのPHP)
<?php fscanf(STDIN,'%d %d',$N,$M); $buy_num = 0; $money = 0; $a_list = []; $b_list = []; for($i=0;$i<$N;++$i){ fscanf(STDIN,'%d %d',$a,$b); array_push($a_list, $a); array_push($b_list, $b); } echo $money;
C(使い慣れた Ruby)
N,M = readline.chomp.split.map(&:to_i) ary = [] left = M money = 0 (1..N).each do |i| hash = {} a,b = readline.chomp.split.map(&:to_i) hash['a'] = a hash['b'] = b ary.push(hash) end ary.sort_by! {|v| v['a'] } ary.each do |set| while((set['a'] > 0) && (set['b'] > 0)) do set['b'] -= 1 left -= 1 money += set['a'] break if left == 0 end break if left == 0 end p money
D(使い慣れた Ruby)=> TLE
def f(res,i) return res ^ i end A,B = readline.chomp.split.map(&:to_i) res = A # result = (B - A) % 2 == 0 ? f(A,B) - 1 : f(A,B) # p result (A+1..B).each do |i| res = f(res,i) end j = 0 ary = [] for i in (A+1..B) ary.push(i ^ (i+1)) if (j % 2 == 0) j += 1 end p res
D (使い慣れた Ruby)=> 終わってから解いた
A,B = readline.chomp.split.map(&:to_i) if(A % 2 == 0) case ((B-A) % 4) when 1 then p 1 when 2 then p 1 ^ B when 3 then p 0 when 0 then p B end else case ((B-A) % 4) when 1 then p A ^ B when 2 then p A ^ 1 when 3 then p A ^ 1 ^ B when 0 then p A end end