結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー InTheBloom
提出日時 2026-08-30 14:04:28
言語 D
(dmd 2.113.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 38 ms / 2,000 ms
+ 589µs
コード長 1,456 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,140 ms
コンパイル使用メモリ 193,920 KB
実行使用メモリ 19,768 KB
最終ジャッジ日時 2026-08-30 14:06:26
合計ジャッジ時間 6,169 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std;

void main () {
    auto N = readln.chomp.map!(a => a - '0').array;
    const int L = N.length.to!int;

    const dig = [4, 5];

    bool[Tuple!(int, bool)] memo;
    bool dp (int pos, bool less) {
        auto key = tuple(pos, less);
        if (key in memo) {
            return memo[key];
        }
        if (pos == L) {
            return true;
        }
        bool ret = false;

        foreach (nex; dig) {
            if (!less && N[pos] < nex) {
                continue;
            }
            bool nless = less || nex < N[pos];

            ret = ret || dp(pos + 1, nless);
        }
        return memo[key] = ret;
    }

    bool less = false;
    auto ans = new int[](0);
    bool fi = false;

    foreach (i; 0 .. L) {
        foreach_reverse (nex; dig) {
            if (!less && N[i] < nex) {
                continue;
            }
            bool nless = less || nex < N[i];

            if (dp(i + 1, nless)) {
                if (i == 0) {
                    fi = true;
                }
                ans ~= nex;
                less = nless;
                break;
            }
        }
    }

    if (fi) {
        writefln("%(%s%)", ans);
    }
    else {
        writeln(replicate("5", L - 1));
    }
}

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