Codeforces Round #147 (Div. 2)

http://www.codeforces.com/contest/237

A,B問題だけ解けた。

A. Free Cash

同時刻に来店するお客の最大数を求めるだけ。

n = gets.chomp.to_i

hash = {}
n.times do
  v = gets.chomp
  if hash.has_key? v
    hash[v] += 1
  else
    hash[v] = 1
  end
end

puts hash.values.max

B. Young Table

ソートして並び替えするだけ。きれいに書けず。

n = gets.chomp.to_i
c = gets.chomp.split(" ").map{|e| e.to_i}
tables = []
map = {}
1.upto(n) do |y|
  col = gets.chomp.split(" ").map{|e| e.to_i}
  tables << col
  
  col.each_with_index do |num, x|
    map[num] = [y, x + 1]
  end  
end
ans = []
k = 0
idx = 1
for i in 1 .. n do
  for j in 1 .. c[i - 1] do
    k += 1
    next if tables[i - 1][j - 1] == k
    
    y = map[k].first
    x = map[k].last
    
    ans << [i, j, y, x]
    
    # swap
    tmp = tables[i - 1][j - 1]
    tables[i - 1][j - 1] = k
    tables[y - 1][x - 1] = tmp    
    
    map[k] = [i, j]
    map[tmp] = [y, x]    
  end
end
puts ans.length
ans.each do |line|
  puts line.join(" ")
end