Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active June 27, 2017 02:21
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 whatalnk/71f510bbc8e1c414420ccb9bf6198872 to your computer and use it in GitHub Desktop.
Save whatalnk/71f510bbc8e1c414420ccb9bf6198872 to your computer and use it in GitHub Desktop.
AtCoder ABC #057 [Ruby]
# A. Remaining Time
a, b = gets.chomp.split(" ").map(&:to_i)
if (a + b) >= 24 then
puts (a + b) - 24
else
puts (a + b)
end
# B. Checkpoints
n, m = gets.chomp.split(" ").map(&:to_i)
ab = []
n.times do
ab << gets.chomp.split(" ").map(&:to_i)
end
cd = []
m.times do
cd << gets.chomp.split(" ").map(&:to_i)
end
ab.each do |a, b|
cp = cd.map{|c, d| (a - c).abs + (b - d).abs}
puts cp.index(cp.min) + 1
end
# C. Digits in Multiplication
n = gets.chomp.to_i
upper = Math.sqrt(n).to_i
ret = n.to_s.length
(1..upper).each do |i|
if n % i == 0 then
curr = ([i, n / i].max).to_s.length
ret = [ret, curr].min
end
end
puts ret
# D. Maximum Average Sets
n, a, b = gets.chomp.split(" ").map(&:to_i)
v = gets.chomp.split(" ").map(&:to_i).sort.reverse
def comb_table(n)
ct = Array.new(51){Array.new(51, 0)}
(0..n).each do |i|
(0..i).each do |j|
if j == 0 || j == i then
ct[i][j] = 1
else
ct[i][j] = ct[i-1][j-1] + ct[i-1][j]
end
end
end
return ct
end
ct = comb_table(n)
ave = v[0..(a-1)].inject(:+) / a.to_f
puts sprintf("%f", ave)
a_th_val_num = 0
a_th_val_pos = 0
n.times do |i|
if v[i] == v[a-1] then
a_th_val_num += 1
if i < a then
a_th_val_pos += 1
end
end
end
cnt = 0
if v[0] == v[a-1] then
(a..b).each do |i|
cnt += ct[a_th_val_num][i]
end
else
cnt += ct[a_th_val_num][a_th_val_pos]
end
puts cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment