結果

問題 No.1470 Mex Sum
ユーザー kokatsu
提出日時 2021-10-18 00:12:59
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 670 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 212,000 KB
実行使用メモリ 10,832 KB
最終ジャッジ日時 2024-06-22 12:49:50
合計ジャッジ時間 5,474 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    int N;
    readf("%d\n", N);

    auto A = readln.chomp.split.to!(int[]);

    auto one = iota(0, N).map!(a => A[a] == 1).cumulativeFold!"a + b".array;
    auto two = iota(0, N).map!(a => A[a] == 2).cumulativeFold!"a + b".array;

    long res;
    foreach (i; 0 .. N-1) {
        int rem = N - i - 1;
        int cnt1 = one[N-1] - one[i];
        int cnt2 = two[N-1] - two[i];
        if (A[i] == 1) {
            res += cnt2 * 3 + (rem - cnt2) * 2; 
        }
        else if (A[i] == 2) {
            res += cnt1 * 3 + (rem - cnt1);
        }
        else {
            res += cnt1 * 2 + (rem - cnt1);
        }
    }

    res.writeln;
}
0