結果

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

ソースコード

diff #

def is_valid(a, b, c):
    if a == b or b == c or a == c:
        return False
    return (b > a and b > c) or (b < a and b < c)

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

if d == 0:
    if is_valid(H[0], H[1], H[2]):
        print(0)
    else:
        print(-1)
    exit()

min_total = float('inf')

# Pattern 1: Middle is the largest
for k2 in range(0, 201):
    H2_new = H[1] - k2 * d
    if H2_new <= 0:
        continue
    # Compute k1_min for H1_new < H2_new
    numerator = H[0] - H2_new + 1
    k1_min = max(0, (numerator + d - 1) // d)
    H1_new = max(0, H[0] - k1_min * d)
    if H1_new >= H2_new:
        continue  # Not satisfy H1_new < H2_new
    
    # Compute k3_min for H3_new < H2_new
    numerator = H[2] - H2_new + 1
    k3_min = max(0, (numerator + d - 1) // d)
    H3_new = max(0, H[2] - k3_min * d)
    if H3_new >= H2_new:
        continue  # Not satisfy H3_new < H2_new
    
    if H1_new != H3_new:
        total = k1_min + k2 + k3_min
        if total < min_total:
            min_total = total
    else:
        if H1_new == 0:
            continue
        # Try adding 1 to k1
        new_H1 = max(0, H1_new - d)
        if new_H1 != H3_new:
            total_candidate = k1_min + 1 + k2 + k3_min
            if total_candidate < min_total:
                min_total = total_candidate
        else:
            # Try adding 1 to k3
            new_H3 = max(0, H3_new - d)
            if new_H3 != H1_new:
                total_candidate = k1_min + k2 + k3_min + 1
                if total_candidate < min_total:
                    min_total = total_candidate

# Pattern 2: Middle is the smallest
for k2 in range(0, 201):
    H2_new = H[1] - k2 * d
    if H2_new < 0:
        continue
    # Check if H0 and H2 are both larger than H2_new
    if H[0] <= H2_new or H[2] <= H2_new:
        continue
    # Check if H0 and H2 are different
    if H[0] != H[2]:
        total = k2
        if total < min_total:
            min_total = total
    else:
        # Need to make them different by adding one spell to either
        new_H0 = max(0, H[0] - d)
        new_H2 = max(0, H[2] - d)
        # Check if after adding one spell, they are different and both > H2_new
        if new_H0 > H2_new and new_H2 > H2_new and new_H0 != new_H2:
            total = k2 + 1
            if total < min_total:
                min_total = total
        else:
            # Check each possibility
            if new_H0 > H2_new and new_H0 != H[2]:
                total = k2 + 1
                if total < min_total:
                    min_total = total
            elif new_H2 > H2_new and new_H2 != H[0]:
                total = k2 + 1
                if total < min_total:
                    min_total = total

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