結果

問題 No.966 引き算をして門松列(その1)
ユーザー kimiyuki
提出日時 2020-01-13 21:03:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 807 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 79,468 KB
最終ジャッジ日時 2024-12-22 20:43:06
合計ジャッジ時間 2,444 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import math

def iterate(a, b, c):
    for a in range(a - 3, a + 1):
        for b in range(b - 3, b + 1):
            for c in range(c - 3, c + 1):
                yield a, b, c
                yield min(b - 1, a), b, min(b - 2, c)
                yield min(b - 2, a), b, min(b - 1, c)
                yield a, min(a - 1, b), c
                yield a, min(c - 1, b), c

def solve(a, b, c):
    ans = math.inf
    for a1, b1, c1 in iterate(a, b, c):
        if 1 <= a1 and 1 <= b1 and 1 <= c1:
            if a1 != c1 and ((a1 < b1 and b1 > c1) or (a1 > b1 and b1 < c1)):
                ans = min(ans, a - a1 + b - b1 + c - c1)
    if math.isinf(ans):
        return -1
    return ans

for _ in range(int(input())):
    a, b, c = map(int, input().split())
    print(solve(a, b, c))
0