結果

問題 No.2042 RGB Caps
ユーザー simansiman
提出日時 2022-08-20 18:32:11
言語 Ruby
(3.4.1)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 2,311 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2024-11-16 02:01:04
合計ジャッジ時間 4,407 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:11: warning: assigned but unused variable - a
Syntax OK

ソースコード

diff #

N, K = gets.split.map(&:to_i)
Q = K.times.map { a, c = gets.chomp.split; [a.to_i, c] }
Q.sort_by! { |a, c| a }

ans = []
counter = Hash.new(0)

1.upto(N) do |n|
  if Q.size > 0
    if n == Q[0][0]
      a, c = Q.shift

      case c
      when 'R'
        if counter['R'] == counter['G'] && counter['G'] == counter['B']
          ans << 'R'
          counter['R'] += 1
        else
          if counter['G'] < counter['R']
            ans << 'G'
            counter['G'] += 1
          elsif counter['B'] < counter['R']
            ans << 'B'
            counter['B'] += 1
          else
            ans << 'R'
            counter['R'] += 1
          end
        end
      when 'G'
        if counter['R'] == counter['G'] && counter['G'] == counter['B']
          ans << 'G'
          counter['G'] += 1
        else
          if counter['R'] < counter['G']
            ans << 'R'
            counter['R'] += 1
          elsif counter['B'] < counter['G']
            ans << 'B'
            counter['B'] += 1
          else
            ans << 'G'
            counter['G'] += 1
          end
        end
      when 'B'
        if counter['R'] == counter['G'] && counter['G'] == counter['B']
          ans << 'B'
          counter['B'] += 1
        else
          if counter['R'] < counter['B']
            ans << 'R'
            counter['R'] += 1
          elsif counter['G'] < counter['B']
            ans << 'G'
            counter['G'] += 1
          else
            ans << 'B'
            counter['B'] += 1
          end
        end
      end
    else
      if counter['R'] < counter['G'] && counter['R'] < counter['B']
        ans << 'R'
        counter['R'] += 1
      elsif counter['G'] < counter['R'] && counter['G'] < counter['B']
        ans << 'G'
        counter['G'] += 1
      elsif counter['B'] < counter['R'] && counter['B'] < counter['G']
        ans << 'B'
        counter['B'] += 1
      else
        if counter['R'] < counter['G'] || counter['R'] < counter['B']
          ans << 'R'
          counter['R'] += 1
        elsif counter['G'] < counter['R'] || counter['G'] < counter['B']
          ans << 'G'
          counter['G'] += 1
        else
          ans << 'B'
          counter['B'] += 1
        end
      end
    end
  else
    ans << 'R'
    counter['R'] += 1
  end
end

puts ans.join
0