結果

問題 No.281 門松と魔法(1)
ユーザー ckawatak
提出日時 2019-02-16 14:31:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 39 ms / 1,000 ms
コード長 978 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 81,960 KB
実行使用メモリ 54,220 KB
最終ジャッジ日時 2024-09-22 05:40:41
合計ジャッジ時間 4,085 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

D = int(input())
H1 = int(input())
H2 = int(input())
H3 = int(input())

def is_kadomatsu(h1, h2, h3):
    return h1 != h3 and ((h1 < h2 and h3 < h2) or (h2 < h1 and h2 < h3))

def magic(h):
    return max(0, h-D)

def solve():
    if D == 0:
        return 0 if is_kadomatsu(H1,H2,H3) else -1

    result = float('inf')
    
    # maximize h2
    h1 = H1
    h2 = H2
    h3 = H3
    x = max(0, (h1-h2+D)//D)
    y = max(0, (h3-h2+D)//D)
    h1 = max(0, h1-x*D)
    h3 = max(0, h3-y*D)
    if h1 == h3 and h1 != 0:
        h1 = magic(h1)
        x = x + 1
    if is_kadomatsu(h1,h2,h3):
        result = min(result, x+y)

    # minimize h2
    h1 = H1
    h2 = H2
    h3 = H3
    x = 0
    if h1 == h3:
        h1 = magic(h1)
        x = x + 1
    shorter = min(h1,h3)
    y = max(0, (h2-shorter+D)//D)
    h2 = max(0, h2-y*D)
    if is_kadomatsu(h1,h2,h3):
        result = min(result, x+y)

    if result == float('inf'):
        result = -1

    return result

print(solve())
0