公式

D - カードバトル / Card Battle 解説 by admin

gpt-5.5-high

Overview

Takahashi can defeat Aoki’s cards up to the point where his maximum card can defeat them.
That is, if the maximum value of \(A\) is \(X\), we examine \(B_1, B_2, \ldots\) in order, and the answer is the count of cards we can defeat before encountering the first card strictly greater than \(X\).

Analysis

The key point of this game is that the winning card remains at the top of the deck.

Let the maximum value of Takahashi’s cards be

\[ X = \max(A_1, A_2, \ldots, A_N) \]

Since Takahashi can freely arrange his deck at the beginning, he can place the card with the maximum value \(X\) at the top of his deck.

Then, for each of Aoki’s cards \(B_j\):

  • If \(B_j \leq X\), Takahashi can win with his maximum card.
  • When he wins, that card is not discarded and remains at the top of the deck.

Therefore, as long as \(B_j \leq X\), he can continue to defeat any number of cards using the same maximum card.

On the other hand, consider the case where a card \(B_j\) satisfies

\[ B_j > X \]

In this case, Takahashi cannot defeat \(B_j\) using any of his cards.
This is because \(X\) is the maximum value among all of Takahashi’s cards.

Therefore, no matter how he uses the replacement operations, it is impossible to defeat this \(B_j\).
Every time Takahashi’s card loses, it is discarded, while Aoki’s \(B_j\) remains on the field, meaning Takahashi cannot proceed to Aoki’s subsequent cards.

In other words, the answer is:

The number of consecutive cards from the beginning of \(B\) that are less than or equal to \(\max A\).

The number of replacement operations \(K\) is not needed in this optimal strategy. It is sufficient to place the maximum card at the top of the deck from the beginning.

Naively simulating the game or searching for how to use the replacement operations would be complicated, but in reality, it is sufficient to look only at the maximum value.

Algorithm

  1. Find the maximum value of Takahashi’s cards, \(X = \max A\).
  2. Examine Aoki’s cards \(B_1, B_2, \ldots, B_M\) in order.
  3. If \(B_j \leq X\), it can be defeated, so increment the answer by \(1\).
  4. When \(B_j > X\) first appears, terminate the process as no further cards can be defeated.
  5. Output the answer.

For example, if

\[ A = [3, 10, 5] \]

the maximum value is \(10\).

If

\[ B = [4, 8, 10, 12, 1] \]

we can defeat \(4, 8, 10\), but we cannot defeat \(12\).
Therefore, the answer is \(3\).

Complexity

  • Time Complexity: \(O(N + M)\)
  • Space Complexity: \(O(1)\)

Implementation Details

There is no need to manage all of Takahashi’s cards in detail; we only need to find the maximum value.

max_a = max(map(int, input().split()))

After that, examine Aoki’s cards in order and count them if they are less than or equal to the maximum value.

for b in map(int, input().split()):
    if b <= max_a:
        ans += 1
    else:
        break

As soon as a card greater than max_a appears for the first time, it is impossible to defeat any more cards, so we break.

Source Code

import sys

input = sys.stdin.buffer.readline

N, M, K = map(int, input().split())
max_a = max(map(int, input().split()))

ans = 0
for b in map(int, input().split()):
    if b <= max_a:
        ans += 1
    else:
        break

print(ans)

This editorial was generated by gpt-5.5-high.