Skip to content

Instantly share code, notes, and snippets.

@jenya239
Created February 16, 2017 20:16
Show Gist options
  • Save jenya239/87d57ad264f64967f86d5c7873b670b1 to your computer and use it in GitHub Desktop.
Save jenya239/87d57ad264f64967f86d5c7873b670b1 to your computer and use it in GitHub Desktop.
data = STDIN.read
data = data.scan( /\d+/ ).map{ |is| is.to_i }
N = data.shift
K = data.shift
cables = data.clone
max = cables.max
max2 = 2 ** (Math::log(max+1) / Math::log(2)).ceil
l = step = max2 / 2
max_l = 0
while step > 0 do
count = 0
cables = data.clone
cables.each do |cable|
while cable >= l do
cable -= l
count += 1
cable.to_s + ' ' + l.to_s + ' ' + count.to_s
break if count == K
end
break if count == K
end
step /= 2
if count == K
max_l = l
l += step
elsif
l -= step
end
end
print max_l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment