Skip to content

Instantly share code, notes, and snippets.

@parrot-studio
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parrot-studio/10815136 to your computer and use it in GitHub Desktop.
Save parrot-studio/10815136 to your computer and use it in GitHub Desktop.
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
@parrot-studio
Copy link
Author

dad77a => http://paiza.jp/poh/paizen/result/a57e0c23cf40a789921665b57152a61a

ついに突破
bit演算とparse回数の最適化が鍵であった

@parrot-studio
Copy link
Author

最終的な解答だけ残して削除

@parrot-studio
Copy link
Author

6eb329 => http://paiza.jp/poh/paizen/result/0a1301346eadae7ae26515e64a786e7f

evalなんて馬鹿げた手を使っていたら、そりゃ遅くなるよ(´・ω・)(・ω・`)ネー

@parrot-studio
Copy link
Author

3650f8 => http://paiza.jp/poh/paizen/result/82967b508c0f18216f02892f36adb69d

びっくりするほど変わらなかったΣ(゚Д゚)ガーン

@parrot-studio
Copy link
Author

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