結果

問題 No.126 2基のエレベータ
ユーザー gew1fw
提出日時 2025-06-12 12:49:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,034 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 54,372 KB
最終ジャッジ日時 2025-06-12 12:49:33
合計ジャッジ時間 2,424 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

A, B, S = map(int, input().split())

def get_candidate_down(A_floor, B_floor, S_floor):
    candidates = []
    # A is always a candidate
    a_dist = abs(A_floor - S_floor)
    candidates.append(('A', a_dist))
    # B is a candidate only if S is not 1
    if S_floor != 1:
        if B_floor >= S_floor:
            b_dist = B_floor - S_floor
        else:
            b_dist = S_floor - B_floor
        candidates.append(('B', b_dist))
    # Find the candidate with minimum distance, A has priority in case of tie
    min_dist = min([d for _, d in candidates])
    filtered = [e for e, d in candidates if d == min_dist]
    if 'A' in filtered:
        return 'A'
    else:
        return 'B' if filtered else 'A'

def get_candidate_up(A_floor, B_floor, S_floor):
    candidates = []
    # Calculate distance for A
    if A_floor <= S_floor:
        a_dist = S_floor - A_floor
    else:
        a_dist = A_floor - S_floor
    candidates.append(('A', a_dist))
    # Calculate distance for B
    if B_floor <= S_floor:
        b_dist = S_floor - B_floor
    else:
        b_dist = B_floor - S_floor
    candidates.append(('B', b_dist))
    # Find the candidate with minimum distance, A has priority in case of tie
    min_dist = min([d for _, d in candidates])
    filtered = [e for e, d in candidates if d == min_dist]
    if 'A' in filtered:
        return 'A'
    else:
        return 'B'

def compute_cost(elevator, A_floor, B_floor, S_floor):
    if elevator == 'A':
        return abs(A_floor - S_floor) + S_floor
    else:
        # Check if B can reach A's current floor (A_floor must be >=1)
        if A_floor == 0:
            return float('inf')
        else:
            return abs(B_floor - S_floor) + abs(S_floor - A_floor) + A_floor

# Calculate for down button
elevator_down = get_candidate_down(A, B, S)
cost_down = compute_cost(elevator_down, A, B, S)

# Calculate for up button
elevator_up = get_candidate_up(A, B, S)
cost_up = compute_cost(elevator_up, A, B, S)

min_cost = min(cost_down, cost_up)

print(min_cost)
0