結果

問題 No.1470 Mex Sum
ユーザー magurofly
提出日時 2021-04-09 21:45:59
言語 Ruby
(3.4.1)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 58 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 37,632 KB
最終ジャッジ日時 2024-06-25 04:53:01
合計ジャッジ時間 14,312 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 49
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Range
    def upper_bound
        ac, wa = self.begin, self.end
        wa += 1 unless self.exclude_end?
        while ac + 1 < wa
            wj = (ac + wa) / 2
            if yield(wj)
                ac = wj
            else
                wa = wj
            end
        end
        ac
    end
end

N = gets.to_i
A = gets.split.map(&:to_i)

counts = [[0, 0]]
A.each do |x|
    a, b = counts[-1]
    case x
    when 1
        counts << [a + 1, b]
    when 2
        counts << [a, b + 1]
    else
        counts << [a, b]
    end
end

ans = 0
(0 .. N - 2).each do |l|
    ones = counts[N][0] - counts[l][0]
    twos = counts[N][1] - counts[l][1]
    others = N - l - ones - twos - 1
    case A[l]
    when 1
        ans += (ones + others) * 2 + twos * 3
    when 2
        ans += (twos + others) * 1 + ones * 3
    else
        ans += (twos + others) * 1 + ones * 2
    end
end

puts ans
0