結果

問題 No.2673 A present from B
ユーザー InTheBloomInTheBloom
提出日時 2024-03-15 23:39:21
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,802 bytes
コンパイル時間 3,812 ms
コンパイル使用メモリ 171,104 KB
実行使用メモリ 13,480 KB
最終ジャッジ日時 2024-03-15 23:39:29
合計ジャッジ時間 7,284 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 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 59 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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; 1..N+1) PQ.insert(tuple(j, dp[i + 1][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