Created
May 6, 2013 05:19
-
-
Save coderek/5523493 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 1B problem B 2013 Google Code Jam | |
# adapted answer from 'dolphinigle' | |
gets.to_i.times do |ca| | |
my, n = gets.split(" ").map(&:to_i) | |
motes = gets.split(" ").map(&:to_i).sort | |
best = motes.length # all delete | |
if my == 1 | |
puts "Case ##{ca+1}: #{best}" | |
else | |
add = 0 | |
motes.each_with_index do |o, i| | |
# just eat and go next | |
my += o and next if my > o | |
# stop here, update the best before add | |
# important step! as it will catch the case where add and delete are mixed together | |
best = [best, add + n - i].min | |
# add | |
my += my - 1 and add += 1 while o >= my | |
my += o | |
end | |
best = [best, add].min | |
puts "Case ##{ca+1}: #{best}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment