Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active May 16, 2017 16:20
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/68d8be003b93d5e4ed06ce05693c561f to your computer and use it in GitHub Desktop.
Save whatalnk/68d8be003b93d5e4ed06ce05693c561f to your computer and use it in GitHub Desktop.
AtCoder ABC #060
a, b, c = gets.chomp.split(" ")
if a[-1] == b[0] && b[-1] == c[0] then
puts "YES"
else
puts "NO"
end
a, b, c = gets.chomp.split(" ").map(&:to_i)
x = a
h = Hash.new()
while true
k = (x + c) % b
break if h.has_key?(k)
h[k] = 1
x += a
end
if h.has_key?(0) then
puts "YES"
else
puts "NO"
end
N, T = gets.chomp.split(" ").map(&:to_i)
tt = gets.chomp.split(" ").map(&:to_i)
ret = T
(N-1).times do |i|
ret += [tt[i+1] - tt[i], T].min
end
puts ret
# 2017-05-16
N, W = gets.chomp.split(" ").map(&:to_i)
k1 = []
k2 = []
k3 = []
k4 = []
w1, v1 = gets.chomp.split(" ").map(&:to_i)
k1 << v1
(N - 1).times do
w, v = gets.chomp.split(" ").map(&:to_i)
case w
when w1
k1 << v
when w1 + 1
k2 << v
when w1 + 2
k3 << v
when w1 + 3
k4 << v
end
end
k1.sort_by!{|a| -a}
(k1.length - 1).times do |i|
k1[i+1] += k1[i]
end
k2.sort_by!{|a| -a}
(k2.length - 1).times do |i|
k2[i+1] += k2[i]
end
k3.sort_by!{|a| -a}
(k3.length - 1).times do |i|
k3[i+1] += k3[i]
end
k4.sort_by!{|a| -a}
(k4.length - 1).times do |i|
k4[i+1] += k4[i]
end
def take(ary, n)
if n == 0 then
return 0
else
return ary[n-1]
end
end
sum = 0
(k1.length + 1).times do |i|
(k2.length + 1).times do |j|
(k3.length + 1).times do |k|
(k4.length + 1).times do |l|
if w1 * i + (w1 + 1) * j + (w1 + 2) * k + (w1 + 3) * l <= W then
sum = [sum, take(k1, i) + take(k2, j) + take(k3, k) + take(k4, l)].max
end
end
end
end
end
puts sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment