Last active
August 29, 2015 13:59
-
-
Save parrot-studio/10815136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height, width = gets.split(' ').map(&:to_i) | |
# bitとして扱う | |
lines = [] | |
(1..height).each do |h| | |
lines << gets.chomp.to_i(2) | |
end | |
# 対象を解析 | |
targets = {} | |
tickets = [] | |
gets.to_i.times do | |
wh, ww = gets.split(' ').map(&:to_i) | |
tickets << [ww, wh] | |
targets[ww] = true if ww <= width | |
end | |
# 盤面をparse | |
memo = Hash.new{|h,k| h[k] = Hash.new(0)} | |
targets.keys.each do |tw| | |
wm = memo[tw] | |
val = ("1" * tw).to_i(2) | |
(width - tw + 1).times do | |
count = 0 | |
height.times do |vi| | |
if lines[vi] & val == 0 | |
count += 1 | |
else | |
(1..count).each{|c| wm[c] += (count - c + 1)} if count > 0 | |
count = 0 | |
end | |
end | |
(1..count).each{|c| wm[c] += (count - c + 1)} if count > 0 | |
val = (val << 1) | |
end | |
end | |
# 最終結果 | |
tickets.each do |key| | |
puts memo[key.first][key.last] | |
end |
f1185c => http://paiza.jp/poh/paizen/result/40c8052ed89a9f28efb21eaf4120d658
Test case 6 : 5.59s
248f7f => http://paiza.jp/poh/paizen/result/5b8db51fd04608f1c7463d268560307b
ver5
Test case 6 : 4.39s
47c8e2 => http://paiza.jp/poh/paizen/result/da003df380f521e46f8d1ae449edb06d
Test case 6 : 4.41s
dad77a => http://paiza.jp/poh/paizen/result/a57e0c23cf40a789921665b57152a61a
ついに突破
bit演算とparse回数の最適化が鍵であった
最終的な解答だけ残して削除
6eb329 => http://paiza.jp/poh/paizen/result/0a1301346eadae7ae26515e64a786e7f
evalなんて馬鹿げた手を使っていたら、そりゃ遅くなるよ(´・ω・)(・ω・`)ネー
3650f8 => http://paiza.jp/poh/paizen/result/82967b508c0f18216f02892f36adb69d
びっくりするほど変わらなかったΣ(゚Д゚)ガーン
9b15a2 => http://paiza.jp/poh/paizen/result/0ba0a0c1b970fb72525886d3abc42e15
ちょっといじったらSSSに到達(; д ) ゚ ゚
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
219a57 => http://paiza.jp/poh/paizen/result/39a0de82f850ba3b155d60600e6474ad
Test case 6 : 5.88s
オブジェクトレベルのチューニングになってきた