結果

問題 No.968 引き算をして門松列(その3)
ユーザー gew1fw
提出日時 2025-06-12 16:29:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,773 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 84,056 KB
最終ジャッジ日時 2025-06-12 16:29:32
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

def is_gate(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return False
    if a == b or b == c or a == c:
        return False
    # Case 1: B > A > C
    if b > a and a > c:
        return True
    # Case 2: B > C > A
    if b > c and c > a:
        return True
    # Case 3: A > C > B
    if a > c and c > b:
        return True
    # Case 4: C > A > B
    if c > a and a > b:
        return True
    return False

def compute_case1(A, B, C, X, Y, Z):
    # Case 1: B' > A' > C'
    # Assume B' = A' +1, A' = C' +1
    # So B' = C' + 2
    # Express in terms of x, y, z:
    # B - x - y = A - x - z + 1 → solve for z
    # A - x - z = C - y - z + 1 → solve for y
    # y = x + (C - A) + 1
    # z = y - (B - A) + 1
    # Now, find x, y, z such that all are >=0 and a, b, c >0
    # Iterate x from 0 to possible maximum and check
    # But since it's time-consuming, let's find x that can satisfy the constraints
    # But for the sake of time, let's try x=0 first
    x = 0
    y = (C - A) + 1
    z = y - (B - A) + 1
    if y < 0 or z < 0:
        return None
    a = A - x - z
    b = B - x - y
    c = C - y - z
    if a <= 0 or b <= 0 or c <= 0:
        return None
    if b > a and a > c:
        cost = x * X + y * Y + z * Z
        return cost
    else:
        return None

def compute_case2(A, B, C, X, Y, Z):
    # Case 2: B' > C' > A'
    # Assume B' = C' +1, C' = A' +1 → B' = A' + 2
    # Express in terms of x, y, z:
    # B - x - y = C - y - z +1 → B - x - y = C - y - z + 1 → B -x = C - z + 1 → z = C - B + x + 1 - 1 → z = C - B + x
    # C - y - z = A -x - z +1 → C - y = A -x +1
    # y = C - (A - x + 1) → y = C - A + x - 1
    # Now, check constraints
    # Trying x=0:
    x = 0
    y = C - A + x - 1
    z = C - B + x
    if y < 0 or z < 0:
        return None
    a = A - x - z
    b = B - x - y
    c = C - y - z
    if a <= 0 or b <= 0 or c <= 0:
        return None
    if b > c and c > a:
        cost = x * X + y * Y + z * Z
        return cost
    else:
        return None

def compute_case3(A, B, C, X, Y, Z):
    # Case 3: A' > C' > B'
    # Assume A' = C' +1, C' = B' +1 → A' = B' + 2
    # Express in terms of x, y, z:
    # A -x - z = C - y - z +1 → A -x = C - y +1 → y = C - A + x + 1
    # C - y - z = B - x - y +1 → C - y - z = B - x - y +1 → C - z = B - x +1 → z = C - B + x -1
    # Trying x=0:
    x = 0
    y = C - A + x + 1
    z = C - B + x -1
    if y < 0 or z < 0:
        return None
    a = A - x - z
    b = B - x - y
    c = C - y - z
    if a <= 0 or b <= 0 or c <= 0:
        return None
    if a > c and c > b:
        cost = x * X + y * Y + z * Z
        return cost
    else:
        return None

def compute_case4(A, B, C, X, Y, Z):
    # Case 4: C' > A' > B'
    # Assume C' = A' +1, A' = B' +1 → C' = B' + 2
    # Express in terms of x, y, z:
    # C - y - z = A -x - z +1 → C - y = A -x +1 → y = C - A + x -1
    # A -x - z = B - x - y +1 → A - z = B - y +1 → y = B - A + z +1
    # Substitute y from first equation into second:
    # C - A + x -1 = B - A + z +1 → C + x -1 = B + z +1 → z = C + x -2 - B
    # Trying x=0:
    x = 0
    y = C - A + x -1
    z = C + x - 2 - B
    if y < 0 or z < 0:
        return None
    a = A - x - z
    b = B - x - y
    c = C - y - z
    if a <= 0 or b <= 0 or c <= 0:
        return None
    if c > a and a > b:
        cost = x * X + y * Y + z * Z
        return cost
    else:
        return None

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    T = int(input[idx])
    idx += 1
    for _ in range(T):
        A = int(input[idx])
        B = int(input[idx+1])
        C = int(input[idx+2])
        X = int(input[idx+3])
        Y = int(input[idx+4])
        Z = int(input[idx+5])
        idx +=6
        
        min_cost = None
        if is_gate(A, B, C):
            print(0)
            continue
        
        # Case 1
        cost = compute_case1(A, B, C, X, Y, Z)
        if cost is not None:
            if min_cost is None or cost < min_cost:
                min_cost = cost
        
        # Case 2
        cost = compute_case2(A, B, C, X, Y, Z)
        if cost is not None:
            if min_cost is None or cost < min_cost:
                min_cost = cost
        
        # Case 3
        cost = compute_case3(A, B, C, X, Y, Z)
        if cost is not None:
            if min_cost is None or cost < min_cost:
                min_cost = cost
        
        # Case 4
        cost = compute_case4(A, B, C, X, Y, Z)
        if cost is not None:
            if min_cost is None or cost < min_cost:
                min_cost = cost
        
        if min_cost is not None:
            print(min_cost)
        else:
            print(-1)

if __name__ == "__main__":
    main()
0