Created
April 15, 2012 12:19
-
-
Save robertmarsal/2392461 to your computer and use it in GitHub Desktop.
Problem B: Dancing With the Googlers
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
file = File.new("input", "r") | |
max = file.gets | |
counter = 1 | |
while (line = file.gets) | |
data = line.split(" ") | |
#num of cases | |
cases = data[0].to_i | |
#num of surprising cases | |
s = data[1].to_i | |
#min result | |
p = data[2].to_i | |
#total points | |
rest = data[3,cases] | |
result = 0 | |
rest.each do |ssum| | |
ssum = ssum.to_i | |
bnum = ssum/3.floor | |
if bnum == p && bnum!=0 && p!=0 | |
#exact division | |
result = result + 1 | |
elsif bnum >= p | |
#it matches | |
result = result + 1 | |
else | |
trest = ssum - (3 * bnum) | |
#if we have any surprising cases left | |
if s > 0 | |
if (bnum + trest) >= p && trest >= 2 | |
result = result + 1 | |
s = s - 1 | |
elsif trest == 0 && bnum != 0 && ssum % bnum == 0 && bnum + 2 > p | |
result = result + 1 | |
s = s - 1 | |
else | |
while trest > 0 | |
bnum = bnum + 1 | |
trest = trest - 3 | |
end | |
bnum >= p ? result = result + 1 : nil | |
end | |
else | |
while trest > 0 | |
bnum = bnum + 1 | |
trest = trest - 3 | |
end | |
bnum >= p ? result = result + 1 : nil | |
end | |
end | |
end | |
puts "Case ##{counter}: #{result}" | |
counter = counter + 1 | |
end | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment