Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created April 12, 2017 03:40
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/5a316e3dbce0152f4de19051192e154a to your computer and use it in GitHub Desktop.
Save whatalnk/5a316e3dbce0152f4de19051192e154a to your computer and use it in GitHub Desktop.
AtCoder ABC #009
n = gets.chomp.to_i
if n % 2 == 0 then
puts n / 2
else
puts n / 2 + 1
end
n = gets.chomp.to_i
prices = []
n.times do
prices << gets.chomp.to_i
end
puts prices.sort.uniq[-2]
n, k = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp.split("")
def diff(s, s2)
return 0 if s.empty? && s2.empty?
h = Hash.new(0)
s.each{|c| h[c] += 1}
s2.each{|c| h[c] -= 1}
return h.values.map{|v| v.abs}.inject(:+) / 2
end
rem = s.dup.sort
ret = []
d = 0
n.times do |i|
rem2 = []
rem.each_with_index do |c, j|
ret << c
dd = 0
if ret[i] != s[i] then
dd = 1
end
if d + dd + diff(s[(i+1)..-1], rem2 + rem[(j+1)..-1]) <= k then
rem.slice!(j)
d += dd
break
else
rem2 << ret.pop
end
end
end
puts ret.join("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment