結果

問題 No.200 カードファイト!
ユーザー qwewe
提出日時 2025-05-14 13:12:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,837 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 60,272 KB
最終ジャッジ日時 2025-05-14 13:13:42
合計ジャッジ時間 2,353 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# Helper function to read a list of integers from a line
def read_ints():
    return map(int, sys.stdin.readline().split())

def solve():
    # Read input values
    N = int(sys.stdin.readline()) # Number of matches
    A_count = int(sys.stdin.readline()) # Number of cards A has
    A_cards_initial = list(read_ints()) # A's cards
    C_count = int(sys.stdin.readline()) # Number of cards C has
    C_cards_initial = list(read_ints()) # C's cards

    # Use tuples for initial card sets to prevent accidental modification
    # and potentially slight performance benefit if hash is needed (not here).
    # Primarily for code clarity/safety.
    A_cards_initial_tuple = tuple(A_cards_initial)
    C_cards_initial_tuple = tuple(C_cards_initial)

    # Initialize current hands with the initial cards
    H_A = list(A_cards_initial_tuple)
    H_C = list(C_cards_initial_tuple)
    
    # Initialize win count for A
    wins = 0

    # Simulate the N matches
    for k in range(N):
        
        # Check if hands are empty and refill if necessary based on game rules
        if not H_A:
            H_A = list(A_cards_initial_tuple)
        if not H_C:
            H_C = list(C_cards_initial_tuple)

        # Sort hands before each match to easily find minimum/maximum cards
        # and apply the greedy strategy.
        H_A.sort()
        H_C.sort()

        # Variable to store information about the best winning pair found for A in this match
        # Format: (value_of_A_card, value_of_C_card, index_of_A_card_in_H_A, index_of_C_card_in_H_C)
        best_win_pair_info = None 
        
        # Indices for the best pair found
        min_b_idx = -1
        min_d_idx = -1
        
        # Strategy: Find a winning pair (A's card > C's card).
        # Prioritize using A's weakest card that can achieve a win.
        # Among the cards C has that A's weakest winning card can beat, choose C's weakest card.
        # This corresponds to finding pair (b, d) such that b > d, b is minimized, and then d is minimized.
        
        # Iterate through A's cards (sorted ascending)
        for b_idx in range(len(H_A)):
            b = H_A[b_idx]
            
            current_min_d_idx_for_this_b = -1
            # Iterate through C's cards (sorted ascending) to find the minimum card 'd' that 'b' can beat
            for d_idx in range(len(H_C)):
                d = H_C[d_idx]
                if b > d:
                    # Found the smallest d this b can beat because H_C is sorted.
                    current_min_d_idx_for_this_b = d_idx
                    break # Stop searching for d for this b, as we found the minimum one.
                # Optimization: If b <= d, then b cannot beat this d or any subsequent cards in sorted H_C.
                # No need to check further d's for this b. We can break the inner loop.
                # However, the explicit check 'b > d' correctly handles this. If b <= d, the condition is false,
                # loop continues to next d. If all d are >= b, loop finishes without finding a win for this b.
            
            # If a suitable d was found for the current b
            if current_min_d_idx_for_this_b != -1:
                 # This pair (b, H_C[current_min_d_idx_for_this_b]) constitutes a win for A.
                 # Since we iterate through b in increasing order (H_A is sorted),
                 # this is the minimal 'b' that can achieve a win.
                 # The found 'd' is the minimal 'd' that this minimal 'b' can beat.
                 # This matches our chosen greedy strategy (Option 1).
                 min_b_idx = b_idx
                 min_d_idx = current_min_d_idx_for_this_b
                 # Store the details of this best pair
                 best_win_pair_info = (H_A[min_b_idx], H_C[min_d_idx], min_b_idx, min_d_idx)
                 # Found the overall best pair according to the strategy, stop searching through A's cards.
                 break 
        
        # Check if a winning pair was found
        if best_win_pair_info:
            # A wins this match. Increment win count.
            wins += 1
            # Unpack the pair info
            b_val, d_val, b_idx, d_idx = best_win_pair_info
            
            # Remove the used cards from hands. Using pop(index) removes the element at that index.
            # Since we remove one element from H_A and one from H_C, index validity is maintained.
            H_A.pop(b_idx) 
            H_C.pop(d_idx)
            
        else: # No winning pair exists for A in this match
            # Implement the sacrifice strategy:
            # A plays their weakest card, C plays their strongest card.
            # This strategy aims to preserve A's stronger cards for future potential wins
            # and remove C's strongest card to make future wins easier.
            
            # Check if hands are non-empty before accessing elements.
            # This check might be redundant due to the refill logic at the start of loop and problem constraints (A, C >= 1),
            # but it's safer programming practice.
            if H_A and H_C:
                 # Index of A's weakest card is 0 because H_A is sorted.
                 b_idx_to_remove = 0 
                 # Index of C's strongest card is the last index because H_C is sorted.
                 d_idx_to_remove = len(H_C) - 1 
            
                 # Remove the sacrificed cards
                 H_A.pop(b_idx_to_remove)
                 H_C.pop(d_idx_to_remove)
            # If somehow a hand became empty mid-match (which shouldn't happen under rules/constraints), 
            # this else block would need careful handling. Assume this won't happen.

    # After N matches, print the total number of wins for A
    print(wins)

# Execute the solve function
solve()
0