結果

問題 No.2673 A present from B
ユーザー InTheBloomInTheBloom
提出日時 2024-03-15 23:49:51
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,950 bytes
コンパイル時間 4,558 ms
コンパイル使用メモリ 171,236 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-15 23:51:49
合計ジャッジ時間 7,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 6 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 462 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 104 ms
6,676 KB
testcase_13 AC 55 ms
6,676 KB
testcase_14 AC 180 ms
6,676 KB
testcase_15 AC 46 ms
6,676 KB
testcase_16 AC 42 ms
6,676 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 8 ms
6,676 KB
testcase_20 AC 157 ms
6,676 KB
testcase_21 WA -
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 WA -
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, M; readln.read(N, M);
    auto A = readln.split.to!(int[]);

    auto dp = new int[][](M + 1, N + 1);
    int[] ans;

    BinaryHeap!(Tuple!(int, int)[], "b[1] < a[1]") PQ = [];

    foreach (bob; 2..N+1) {
        foreach (d; dp) d[] = int.max;

        for (int i = 0; bob + i <= N; i++) dp[0][bob + i] = i;
        for (int i = 0; 1 <= bob - i; i++) dp[0][bob - i] = i;

        foreach (i; 0..M) {
            dp[i + 1][] = dp[i][];

            // 操作
            dp[i + 1][A[i] + 1] = dp[i][A[i]];
            dp[i + 1][A[i]] = dp[i][A[i] + 1];

            // ズル
            foreach (j; 0..2) {
                if (1 <= A[i] - j) PQ.insert(tuple(A[i] - j, dp[i + 1][A[i] - j]));
                if (A[i] + j <= N) PQ.insert(tuple(A[i] + j, dp[i + 1][A[i] + j]));
            }

            while (!PQ.empty) {
                auto h = PQ.front; PQ.removeFront;
                int u = h[0];
                int cost = h[1];

                if (dp[i + 1][u] < cost) continue;
                dp[i + 1][u] = cost;

                // 右となり
                if (u < N) {
                    if (cost + 1 < dp[i + 1][u + 1]) {
                        dp[i + 1][u + 1] = cost + 1;
                        PQ.insert(tuple(u + 1, cost + 1));
                    }
                }

                // 左となり
                if (0 < u) {
                    if (cost + 1 < dp[i + 1][u - 1]) {
                        dp[i + 1][u - 1] = cost + 1;
                        PQ.insert(tuple(u - 1, cost + 1));
                    }
                }
            }
        }

        ans ~= dp[M][1];
    }

    foreach (i, a; ans) {
        write(a, i == ans.length - 1 ? '\n' : ' ');
    }
}

void read (T...) (string S, ref T args) {
    import std.conv : to;
    import std.array : split;
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0