結果

問題 No.126 2基のエレベータ
ユーザー qwewe
提出日時 2025-05-14 12:57:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,892 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,632 KB
実行使用メモリ 54,356 KB
最終ジャッジ日時 2025-05-14 12:58:57
合計ジャッジ時間 2,343 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# Function to read input and calculate the minimum total elevator travel distance
def solve():
    # Read the initial positions of elevator A, elevator B, and Wilson (S)
    # A: position of elevator A (can be 0 to 100, where 0 is B1)
    # B: position of elevator B (can be 1 to 100)
    # S: Wilson's starting position (must be 1 to 100)
    # Floor 0 represents B1 (Basement 1)
    a, b, s = map(int, sys.stdin.readline().split())

    # --- Case 1: Wilson is at floor 1 (S=1) ---
    # Wilson needs to go down to floor 0 (B1).
    # The problem states: "if the down button is pressed on the 1st floor (S=1), ... only elevator A will come".
    # This rule applies regardless of elevator B's position or distance.
    if s == 1:
        # Calculate the total cost for elevator A to serve Wilson:
        
        # 1. Elevator A moves from its current position 'a' to floor 1.
        #    If A is already at floor 1 (a=1), this movement cost is 0.
        #    Otherwise, the cost is the distance |a - 1|.
        cost_a_to_1 = abs(a - 1)
        
        # 2. Wilson gets into elevator A at floor 1.
        # 3. Elevator A moves from floor 1 to floor 0 (B1).
        #    The cost is the distance |1 - 0| = 1.
        cost_1_to_0 = 1
        
        # The total cost is the sum of these two movements by elevator A.
        total_cost = cost_a_to_1 + cost_1_to_0
        print(total_cost)
        # We have found the answer for S=1, so we can exit.
        return 

    # --- Case 2: Wilson is at floor S > 1 ---
    # Wilson needs to call an elevator to floor S first.
    
    # Subcase 2.1: Elevator A is already at Wilson's floor S (a == s)
    # The rule "If an elevator is already at the floor, ... the door just opens" applies.
    if a == s:
        # Elevator A opens its doors (0 movement cost for pickup).
        # Wilson gets into elevator A.
        # Elevator A moves directly from floor S to floor 0 (B1).
        # The cost is the distance |s - 0| = s (since s > 1 >= 0).
        total_cost = s
        print(total_cost)
        # We have found the answer, so we can exit.
        return

    # Subcase 2.2: Elevator B is already at Wilson's floor S (b == s), and Elevator A is not (a != s)
    # The "already there" rule applies for elevator B.
    if b == s:
        # Elevator B opens its doors (0 movement cost for pickup).
        # Wilson gets into elevator B.
        # Since elevator B cannot go to floor 0, Wilson must take B to a floor accessible by A.
        # The most logical floor is floor 1, as it's the closest floor to 0 that B can reach.
        
        # 1. Elevator B moves from floor S to floor 1.
        #    The cost is the distance |s - 1| = s - 1 (since s > 1).
        cost_b_s_to_1 = s - 1
        
        # Wilson gets off elevator B at floor 1.
        # Now, Wilson needs elevator A to go from floor 1 to floor 0.
        # Wilson calls elevator A (using the Down button at floor 1, which guarantees A comes).
        # Elevator A is currently at its initial position 'a'.
        
        # 2. Elevator A moves from position 'a' to floor 1.
        #    The cost is the distance |a - 1|.
        cost_a_to_1 = abs(a - 1)
        
        # Wilson gets into elevator A at floor 1.
        # 3. Elevator A moves from floor 1 to floor 0.
        #    The cost is the distance |1 - 0| = 1.
        cost_a_1_to_0 = 1
        
        # The total cost is the sum of movements by both elevator B and elevator A.
        # total_cost = cost_b_s_to_1 + cost_a_to_1 + cost_a_1_to_0
        # Simplified: total_cost = (s - 1) + abs(a - 1) + 1 = s + abs(a - 1)
        total_cost = s + abs(a - 1) 
        print(total_cost)
        # We have found the answer, so we can exit.
        return

    # Subcase 2.3: Neither elevator A nor B is at Wilson's floor S (a != s and b != s)
    # Wilson presses a button at floor S. The elevator rules determine which one comes.
    # Rule: The closer elevator comes. If distances are equal, A is preferred.
    
    # Calculate the distance from each elevator to floor S.
    dist_a = abs(a - s)
    dist_b = abs(b - s)

    # Compare distances to determine which elevator responds.
    if dist_a <= dist_b:
        # Elevator A comes because it's closer or the distances are equal.
        
        # 1. Elevator A moves from position 'a' to floor S. Cost = dist_a.
        cost_a_to_s = dist_a
        
        # Wilson gets into elevator A at floor S.
        # 2. Elevator A moves from floor S to floor 0. Cost = |s - 0| = s.
        cost_s_to_0 = s
        
        # Total cost is the sum of movements by elevator A.
        total_cost = cost_a_to_s + cost_s_to_0
        print(total_cost)
        # End of this path
    else: # dist_a > dist_b
        # Elevator B comes because it is strictly closer.
        
        # 1. Elevator B moves from position 'b' to floor S. Cost = dist_b.
        cost_b_to_s = dist_b
        
        # Wilson gets into elevator B at floor S. Takes B to floor 1.
        # 2. Elevator B moves from floor S to floor 1. Cost = |s - 1| = s - 1.
        cost_b_s_to_1 = s - 1
        
        # Wilson gets off at floor 1. Calls elevator A (still at initial position 'a').
        # 3. Elevator A moves from position 'a' to floor 1. Cost = |a - 1|.
        cost_a_to_1 = abs(a - 1)
        
        # Wilson gets into elevator A at floor 1.
        # 4. Elevator A moves from floor 1 to floor 0. Cost = |1 - 0| = 1.
        cost_a_1_to_0 = 1
        
        # Total cost is the sum of movements by both B and A.
        # total_cost = cost_b_to_s + cost_b_s_to_1 + cost_a_to_1 + cost_a_1_to_0
        # Simplified: total_cost = dist_b + (s - 1) + abs(a - 1) + 1 = dist_b + s + abs(a - 1)
        total_cost = dist_b + s + abs(a - 1) 
        print(total_cost)
        # End of this path

# Execute the solve function to read input and print the result
solve()
0