Official

C - カードの連結 / Card Concatenation Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

From the cards with numbers greater than or equal to \(X\), we select \(K\) cards prioritizing larger numbers, and arrange them in descending order. By counting the occurrences of each number, we can find the answer without sorting.

Analysis

Noting that all cards have single-digit numbers

According to the constraints, the numbers written on the cards are between \(1\) and \(9\). Therefore, no matter which \(K\) cards we choose, the integer formed by concatenating them will always have \(K\) digits.

For integers with the same number of digits, the one with a larger digit at the first differing position from the left is larger. Therefore, it is optimal to place the largest available digit from left to right.

For example, if \(X=3\), \(K=3\), and the cards are \([2,3,8,5,8]\), the usable cards are \([3,8,5,8]\). Choosing \(8,8,5\) in descending order and arranging them yields the maximum value, \(885\).

Why using larger digits first is valid

When deciding a digit, let \(d\) be the largest unused digit.

If we place a digit smaller than \(d\) at that position, replacing it with \(d\) will make the overall integer strictly larger, regardless of the arrangement of the subsequent digits. If a card with \(d\) is used later, we can simply swap them; if it is not used at all, we can just replace the smaller digit with it.

Therefore, the greedy approach of choosing the largest remaining digit for each position is correct.

Avoiding complete search and sorting

Trying all possible ways to choose and arrange the cards will result in an enormous number of candidates, which will not run in time.

Although sorting all usable cards in descending order would also yield the correct answer, it takes up to \(O(N\log N)\) time. Since there are only \(9\) possible digits here, it is sufficient to just count the frequency of each digit. This allows us to prepare the solution with a single pass over the input.

Algorithm

  1. Initialize an array cnt, which stores the count of each digit, to all \(0\)s.
  2. Read each card’s number \(a\), and increment cnt[a] by \(1\) if \(a \ge X\).
  3. Prepare an empty string answer to store the result.
  4. Iterate over the digits \(d\) in descending order from \(9\) down to \(X\):
    • The number of cards still needed is \(K-|answer|\).
    • Let the number of cards with digit \(d\) to use be \(\min(\mathrm{cnt}[d], K-|answer|)\).
    • Append the digit \(d\) to the end of answer that many times.
    • Terminate once the length of answer reaches \(K\).
  5. If the length of answer is less than \(K\), output -1 because there are not enough usable cards. Otherwise, output answer.

Complexity

  • Time complexity: \(O(N+K)\)
    Counting all cards takes \(O(N)\), and constructing the answer string takes \(O(K)\). The loop checking the digits runs at most \(9\) times. Since \(K \le N\), the overall time complexity can also be expressed as \(O(N)\).
  • Space complexity: \(O(K)\)
    The array for counting frequencies has a constant size, and the answer string stores at most \(K\) characters. Excluding the space to store the answer, the auxiliary space is \(O(1)\).

Implementation Points

  • The condition is “greater than or equal to \(X\),” so numbers equal to \(X\) are also counted.

  • Using min(cnt[d], K - answer.size()) ensures that we do not use more cards than needed.

  • The answer can be printed directly as a string. There is no need to convert it to a numeric type.

  • It is not necessary to store all the input cards; it is sufficient to count their occurrences while reading them.

  • Since \(N\) can be as large as \(10^6\), the provided code uses ios::sync_with_stdio(false) and cin.tie(nullptr) to speed up input/output operations.

    Source Code

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

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

    int N, K, X;
    cin >> N >> K >> X;

    int cnt[10] = {};
    for (int i = 0; i < N; ++i) {
        int a;
        cin >> a;
        if (a >= X) ++cnt[a];
    }

    string answer;
    for (int d = 9; d >= X && (int)answer.size() < K; --d) {
        int take = min(cnt[d], K - (int)answer.size());
        answer.append(take, char('0' + d));
    }

    if ((int)answer.size() < K) {
        cout << -1 << '\n';
    } else {
        cout << answer << '\n';
    }

    return 0;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: