結果

問題 No.281 門松と魔法(1)
ユーザー gew1fw
提出日時 2025-06-12 16:44:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,533 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 54,572 KB
最終ジャッジ日時 2025-06-12 16:44:57
合計ジャッジ時間 4,191 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 52 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

d = int(input())
H = [int(input()) for _ in range(3)]

if d == 0:
    h1, h2, h3 = H
    if h1 != h2 and h2 != h3 and h1 != h3:
        if (h2 > h1 and h2 > h3) or (h2 < h1 and h2 < h3):
            print(0)
        else:
            print(-1)
    else:
        print(-1)
    exit()

from itertools import product

def get_candidates(Hi, d):
    if d == 0:
        return [(0, Hi)]
    candidates = []
    kmax = Hi // d
    for k in range(0, 6):
        h = Hi - k * d
        if h < 0:
            h = 0
        candidates.append((k, h))
    for delta in [-2, -1, 0, 1, 2]:
        k = kmax + delta
        if k < 0:
            continue
        h = Hi - k * d
        if h < 0:
            h = 0
        candidates.append((k, h))
    k_zero = (Hi + d - 1) // d if Hi != 0 else 0
    candidates.append((k_zero, 0))
    h_mod = Hi % d
    candidates.append((kmax, h_mod))
    unique = {}
    for k, h in candidates:
        if h not in unique or k < unique[h][0]:
            unique[h] = (k, h)
    return sorted(unique.values(), key=lambda x: x[0])

candidates1 = get_candidates(H[0], d)
candidates2 = get_candidates(H[1], d)
candidates3 = get_candidates(H[2], d)

min_total = float('inf')

for (k1, h1), (k2, h2), (k3, h3) in product(candidates1, candidates2, candidates3):
    if h1 == h2 or h2 == h3 or h1 == h3:
        continue
    if (h2 > h1 and h2 > h3) or (h2 < h1 and h2 < h3):
        total = k1 + k2 + k3
        if total < min_total:
            min_total = total

print(min_total if min_total != float('inf') else -1)
0