結果

問題 No.968 引き算をして門松列(その3)
ユーザー lam6er
提出日時 2025-03-31 17:43:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,198 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 83,636 KB
最終ジャッジ日時 2025-03-31 17:44:16
合計ジャッジ時間 2,035 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def is_valid(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
    # Check if B is not the median
    sorted_vals = sorted([a, b, c])
    if sorted_vals[1] != b:
        return True
    return False

def compute_case(B_prime, A_prime, C_prime):
    if A_prime < B_prime and C_prime < B_prime:
        return A_prime != C_prime
    elif A_prime > B_prime and C_prime > B_prime:
        return A_prime != C_prime
    else:
        return False

def solve():
    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
        
        # Check if already valid
        if is_valid(A, B, C):
            print(0)
            continue
        
        min_cost = float('inf')
        
        # Case 1: B is max after operations
        # We need B' = B -x - y > A' = A -x - z and > C' = C - y - z
        # Also, A' and C' must be distinct and positive
        
        # Try different possible minimal operations
        # Scenario: use only z operations (x=0, y=0)
        if B > A and B > C:
            z_lower1 = (A - B + 1) if (A >= B) else 0
            z_lower2 = (C - B + 1) if (C >= B) else 0
            z_min = max(z_lower1, z_lower2, 0)
            max_z_allowed = min(A-1, C-1)
            if z_min <= max_z_allowed:
                a_new = A - z_min
                c_new = C - z_min
                if a_new != c_new and B > a_new and B > c_new:
                    cost = z_min * Z
                    min_cost = min(min_cost, cost)
        
        # Scenario: use one Y operation (reduce B and C)
        cost_y = Y
        B1 = B - 1
        C1 = C - 1
        if B1 > 0 and C1 > 0:
            if compute_case(B1, A, C1):
                min_cost = min(min_cost, cost_y)
            else:
                a_new = A
                b_new = B1
                c_new = C1
                if is_valid(a_new, b_new, c_new):
                    min_cost = min(min_cost, cost_y)
        
        # Scenario: use one X operation (reduce A and B)
        cost_x = X
        A1 = A -1
        B1 = B -1
        if A1 > 0 and B1 > 0:
            a_new = A1
            b_new = B1
            c_new = C
            if compute_case(b_new, a_new, c_new) or is_valid(a_new, b_new, c_new):
                min_cost = min(min_cost, cost_x)
        
        # Scenario: use one Z operation (reduce A and C)
        cost_z = Z
        A1 = A -1
        C1 = C -1
        if A1 > 0 and C1 > 0:
            a_new = A1
            b_new = B
            c_new = C1
            if compute_case(b_new, a_new, c_new) or is_valid(a_new, b_new, c_new):
                min_cost = min(min_cost, cost_z)
        
        # Case 2: B is min after operations
        # Check possible scenarios similarly, but this is a more complex case
        
        if min_cost != float('inf'):
            print(min_cost)
        else:
            print(-1)

if __name__ == '__main__':
    solve()
0