結果

問題 No.910 素数部分列
ユーザー siman
提出日時 2020-03-13 14:53:01
言語 Ruby
(3.4.1)
結果
AC  
実行時間 212 ms / 1,000 ms
コード長 494 bytes
コンパイル時間 44 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 24,320 KB
最終ジャッジ日時 2024-11-22 03:15:41
合計ジャッジ時間 10,157 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
S = gets.chomp.chars.map(&:to_i)

ans = 0
counter = Hash.new(0)

S.each do |s|
  if s != 1 && s != 9
    ans += 1
  elsif s == 1
    counter[s] += 1
  elsif s == 9
    if counter[1] > 0
      counter[1] -= 1
      ans += 1
    else
      counter[s] += 1
    end
  end
end

if counter[9] >= 2 && counter[1] >= 1
  a = counter[9] / 2
  b = counter[1]
  c = [a, b].min

  ans += c
  counter[9] -= 2 * c
  counter[1] -= c
end

if counter[1] >= 2
  ans += counter[1] / 2
end

puts ans
0