結果

問題 No.281 門松と魔法(1)
ユーザー しらっ亭しらっ亭
提出日時 2015-09-23 23:39:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 30 ms / 1,000 ms
コード長 1,313 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-24 12:38:32
合計ジャッジ時間 3,002 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,752 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 28 ms
10,752 KB
testcase_04 AC 26 ms
10,880 KB
testcase_05 AC 27 ms
10,880 KB
testcase_06 AC 26 ms
10,752 KB
testcase_07 AC 27 ms
11,008 KB
testcase_08 AC 28 ms
10,880 KB
testcase_09 AC 27 ms
10,880 KB
testcase_10 AC 27 ms
10,880 KB
testcase_11 AC 26 ms
10,880 KB
testcase_12 AC 26 ms
11,008 KB
testcase_13 AC 26 ms
10,880 KB
testcase_14 AC 26 ms
10,880 KB
testcase_15 AC 26 ms
11,008 KB
testcase_16 AC 27 ms
10,880 KB
testcase_17 AC 27 ms
10,880 KB
testcase_18 AC 29 ms
10,880 KB
testcase_19 AC 28 ms
10,880 KB
testcase_20 AC 27 ms
10,752 KB
testcase_21 AC 27 ms
10,880 KB
testcase_22 AC 28 ms
10,752 KB
testcase_23 AC 27 ms
11,008 KB
testcase_24 AC 28 ms
11,008 KB
testcase_25 AC 27 ms
10,752 KB
testcase_26 AC 26 ms
10,880 KB
testcase_27 AC 26 ms
10,752 KB
testcase_28 AC 26 ms
10,752 KB
testcase_29 AC 25 ms
11,008 KB
testcase_30 AC 26 ms
11,008 KB
testcase_31 AC 26 ms
10,880 KB
testcase_32 AC 26 ms
11,008 KB
testcase_33 AC 27 ms
10,880 KB
testcase_34 AC 27 ms
10,880 KB
testcase_35 AC 27 ms
10,752 KB
testcase_36 AC 27 ms
10,880 KB
testcase_37 AC 27 ms
10,880 KB
testcase_38 AC 27 ms
11,008 KB
testcase_39 AC 27 ms
11,008 KB
testcase_40 AC 27 ms
10,880 KB
testcase_41 AC 29 ms
11,008 KB
testcase_42 AC 30 ms
10,752 KB
testcase_43 AC 28 ms
10,752 KB
testcase_44 AC 29 ms
10,880 KB
testcase_45 AC 29 ms
10,880 KB
testcase_46 AC 29 ms
10,880 KB
testcase_47 AC 28 ms
10,880 KB
testcase_48 AC 27 ms
10,752 KB
testcase_49 AC 27 ms
10,752 KB
testcase_50 AC 26 ms
11,008 KB
testcase_51 AC 26 ms
11,008 KB
testcase_52 AC 25 ms
10,880 KB
testcase_53 AC 26 ms
10,752 KB
testcase_54 AC 27 ms
10,752 KB
testcase_55 AC 27 ms
10,880 KB
testcase_56 AC 28 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

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