結果

問題 No.281 門松と魔法(1)
ユーザー しらっ亭
提出日時 2015-09-23 23:39:32
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 35 ms / 1,000 ms
コード長 1,313 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-11-06 20:40:34
合計ジャッジ時間 4,132 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


def next_to(o1, o2, h, d):
    oa = max(o1, o2)
    oi = o1 + o2 - oa
    if oa == h:
        return 1
    elif oa < h:
        t = oa
    elif oi < h:
        t = oi
    else:
        return 1
    ret = (h - t + d - 1) // d
    return ret


def solve():
    d = int(input())

    h1 = int(input())
    h2 = int(input())
    h3 = int(input())

    q = [(0, (h1, h2, h3))]

    while q:
        n, h = heapq.heappop(q)

        zs = h.count(0)
        if zs >= 2:
            continue

        ma = max(h)
        mi = min(h)
        mu = sum(h) - ma - mi
        mic = h.count(mi)

        if h[0] != h[1] and h[0] != h[2] and h[1] != h[2] and h[1] != mu:
            print(n)
            return

        if d == 0:
            print(-1)
            return

        if h[0] != mi or mic != 1:
            m = next_to(h[1], h[2], h[0], d)
            heapq.heappush(q, (n + m, (max(0, h[0] - d * m), h[1], h[2])))

        if (h[1] != mi or mic != 1) and h[1] != ma:
            m = next_to(h[0], h[2], h[1], d)
            heapq.heappush(q, (n + m, (h[0], max(0, h[1] - d * m), h[2])))

        if h[2] != mi or mic != 1:
            m = next_to(h[0], h[1], h[2], d)
            heapq.heappush(q, (n + m, (h[0], h[1], max(0, h[2] - d * m))))

    print(-1)


if __name__ == '__main__':
    solve()
0