Advertisement
dashohoxha

stalls.rb

Apr 9th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.57 KB | None | 0 0
  1. # Solution of this problem:
  2. # https://code.google.com/codejam/contest/3264486/dashboard#s=p2
  3.  
  4. def solve(n, k)
  5.   l = 0
  6.   p2 = 1  # 2**0
  7.   k1 = k
  8.   n1 = n
  9.   chunk = n
  10.   while (k1 - p2 > 0)
  11.     k1 -= p2
  12.     n1 -= p2
  13.     chunk -= 1
  14.     chunk = chunk / 2 + chunk % 2
  15.     l += 1
  16.     p2 *= 2
  17.   end
  18.   chunk -= 1 if k1 > n1 - p2*(chunk - 1)
  19.   chunk -= 1
  20.   sleft = chunk / 2 + chunk % 2
  21.   sright = chunk / 2
  22.   return "#{sleft} #{sright}"
  23. end
  24.  
  25. T = gets.to_i
  26. for t in 1..T
  27.   (n, k) = gets.chomp.split.map { |x| x.to_i }
  28.   ans = solve(n, k)
  29.   puts "Case ##{t}: #{ans}"
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement