Official

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

gpt-5.5-high

Summary

The cards of Aoki that Takahashi can defeat are those from the front of Aoki’s deck \(B\) up to the range that can be defeated by Takahashi’s strongest card.
That is, if the maximum value of \(A\) is \(maxA\), the answer is the number of consecutive cards from the front of \(B\) that satisfy \(B_i \leq maxA\).

Analysis

An important point is that Takahashi can freely decide the order of his deck at the beginning.

Let the maximum value among Takahashi’s cards be

\(maxA = \max(A_1, A_2, \ldots, A_N)\)

Consider placing this card at the very top of his deck.

  • If \(maxA \geq B_i\) for the active card \(B_i\) of Aoki, Takahashi wins.
  • In case of a win, Takahashi’s card is not discarded and remains at the top of his deck.

In other words, by placing the strongest card at the top, all of Aoki’s cards that can be defeated can be defeated with just this single card.

On the other hand, if there is a card \(B_i\) of Aoki such that

\(B_i > maxA\)

Takahashi cannot win against this card using any of his cards.

In this case, Aoki’s card \(B_i\) will remain on the field.
Takahashi’s cards will be discarded each time he loses, but since none of his cards can win, Takahashi’s deck will eventually run out, ending the game.

Therefore, once a card with \(B_i > maxA\) appears, any subsequent cards of Aoki cannot be reached.

For example, if

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

\(B = [4, 8, 10, 11, 1]\)

In this case, \(maxA = 10\).

  • \(4 \leq 10\), so it can be defeated
  • \(8 \leq 10\), so it can be defeated
  • \(10 \leq 10\), so it can be defeated
  • \(11 > 10\), so it cannot be defeated

At this point, the game is essentially over.
Thus, the answer is \(3\).

The number of swap operations \(K\) does not need to be used in this optimal strategy.
It is sufficient to place the strongest card at the top from the beginning.

Algorithm

  1. Read Takahashi’s cards \(A\) and find the maximum value \(maxA\).
  2. Iterate through Aoki’s cards \(B\) in order.
  3. If we have not stopped yet and \(B_i \leq maxA\), it can be defeated, so increment the answer by \(1\).
  4. When \(B_i > maxA\) appears for the first time, subsequent cards cannot be defeated, so mark it as stopped.
  5. Output the final answer.

The answer is the number of consecutive cards from the front of \(B\) that are less than or equal to \(maxA\).

Complexity

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

Implementation Points

There is no need to store the entire arrays \(A\) or \(B\) in memory.

  • For \(A\), we only need to update the maximum value while reading the input.
  • For \(B\), we can also update the answer while reading the inputs sequentially.

However, since we must read the input to the end, even after finding a card that cannot be defeated, we still need to read and discard the remaining elements of \(B\).

Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, K;
    cin >> N >> M >> K;

    long long maxA = 0;
    for (int i = 0; i < N; i++) {
        long long a;
        cin >> a;
        maxA = max(maxA, a);
    }

    int ans = 0;
    bool stopped = false;

    for (int i = 0; i < M; i++) {
        long long b;
        cin >> b;
        if (!stopped && b <= maxA) {
            ans++;
        } else {
            stopped = true;
        }
    }

    cout << ans << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-high.