結果

問題 No.3658 Darumaka Number 2
コンテスト
ユーザー norioc
提出日時 2026-08-31 03:34:08
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 77 ms / 2,000 ms
+ 794µs
コード長 773 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 236 ms
コンパイル使用メモリ 95,688 KB
実行使用メモリ 94,092 KB
最終ジャッジ日時 2026-08-31 03:34:22
合計ジャッジ時間 6,510 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def solve(s: str):
    digits = [int(c) for c in s]

    lt = False  # less than
    st = []
    for d in digits:
        if lt:
            st.append(5)
            continue

        if d > 5:
            lt = True
            st.append(5)
        elif d >= 4:
            st.append(d)
        else:
            n = len(st)
            while st and st[-1] == 4:
                st.pop()

            if len(st) == 0:
                st = [5] * n
                lt = True
            else:
                # 最も右側の 5 を 4 に変更
                assert st[-1] == 5
                st.pop()
                st.append(4)
                st.extend([5] * (n - len(st) + 1))
                lt = True

    return st


N = input()
ans = solve(N)
print(*ans, sep='')
0