公式

C - カードの連結 / Card Concatenation 解説 by admin 印刷ページ

Claude Opus 5 (High)

Overview

We are given \(N\) cards (each with a single-digit integer \(A_i\) written on it). From the cards with values at least \(X\), we must choose exactly \(K\) cards and arrange them in any order to form an integer. The problem asks for the maximum possible integer that can be formed. If there are not enough cards, output \(-1\).

Analysis

Key Observation 1: Every Card Has a “Single Digit”

From the constraint \(1 \leq A_i \leq 9\), every card contains a single-digit number. Therefore, choosing and arranging \(K\) cards will always produce an integer with exactly \(K\) digits. Since the number of digits is the same regardless of which cards are chosen, we do not need to consider “how to choose cards to maximize the number of digits.”

(If cards had multi-digit numbers like \(10\) or \(123\), we would need to compare concatenations lexicographically—such as comparing a+b with b+a to decide whether \(9\) goes before \(10\)—but we do not need to worry about that in this problem.)

Key Observation 2: Prioritizing Higher Digits Is Optimal

To maximize a \(K\)-digit integer of fixed length, we should make the string lexicographically as large as possible. That is:

  • Make the most significant digit as large as possible.
  • If they are the same, make the next digit as large as possible.
  • …

This greedy approach is optimal.

Specifically, we can take all usable cards (those with \(A_i \geq X\)), sort them in descending order, take the largest \(K\) cards, and concatenate them in that order.

Example: When \(A = (3, 9, 5, 9, 1)\), \(K=3\), and \(X=2\), the usable cards are \(\{3,9,5,9\}\). In descending order, they are \(9,9,5,3\), so taking the top \(3\) gives the answer \(995\).

Why is this optimal?: It is always best to place the largest available number at the most significant digit (placing a smaller number there guarantees that the resulting integer is strictly smaller, regardless of subsequent digits). For the next digit, we similarly pick the largest remaining number, and by induction, arranging the top \(K\) cards in descending order yields the maximum integer.

Pitfalls of Naive Approaches

  • Trying all permutations: Since \(K \leq 18\), checking all \(K!\) permutations (\(18! \approx 6.4 \times 10^{15}\)) is completely impossible within the time limit.
  • Storing the answer in an integer type: When \(K = 18\), the answer can have up to \(18\) digits (\(999999999999999999\)). While this barely fits in a \(64\)-bit integer (maximum value \(\approx 9.2 \times 10^{18}\)), it will definitely overflow a \(32\)-bit integer. For safety, it is best to construct and output it as a string.
  • Sorting: Even a comparison-based sort taking \(O(N \log N)\) easily passes within the time limit for \(N \leq 10^6\), but since values are only from \(1\) to \(9\), you can achieve \(O(N)\) using counting sort (bucket sort).

Algorithm

  1. While reading the input, collect only the cards satisfying \(A_i \geq X\) (either add them to an array \(v\) or count their frequencies \(cnt[1..9]\)).
  2. If the number of collected cards is less than \(K\), output \(-1\) and terminate.
  3. Sort the collected cards in descending order, convert the top \(K\) elements to characters, and concatenate them.
  4. Output the resulting string of length \(K\).

For the counting sort variant, iterating \(d = 9, 8, \dots, X\) and appending \(d\) to the string \(cnt[d]\) times (without exceeding the remaining number of needed cards) yields the same result.

Complexity

  • Time Complexity: \(O(N \log N)\) (when using comparison sort) / \(O(N + K)\) (when using counting sort, since the value range is \(1\) to \(9\))
  • Space Complexity: \(O(N)\) (when storing the filtered array) / \(O(1)\) auxiliary space for counting sort (if implemented without storing the input)

Implementation Notes

  • Do not forget the \(-1\) condition: Output \(-1\) if the number of cards with value at least \(X\) is less than \(K\). Do not assume it is valid simply because “\(N \geq K\)”; check the count after filtering.

  • Build the answer as a string: \(K\) is at most \(18\), meaning the answer can have up to \(18\) digits. An int will definitely overflow, and even a long long is close to the boundary. Appending characters like char('0' + v[i]) to a std::string and printing it is both safe and simple.

  • Fast I/O: Since \(N \leq 10^6\), in C++ it is advisable to use scanf or add the following when using cin:

    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    (In Python, use sys.stdin for bulk reading).

  • Saving memory: There is no need to store the entire input; simply push_back only the elements that are at least \(X\) as you read them (using reserve can reduce reallocation overhead).

  • Sorting in descending order can be written as sort(v.begin(), v.end(), greater<int>()).

    Source Code

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

int main(){
    int N, K, X;
    if(!(cin >> N >> K >> X)) return 0;
    vector<int> v;
    v.reserve(N);
    for(int i=0;i<N;i++){
        int a; cin >> a;
        if(a >= X) v.push_back(a);
    }
    if((int)v.size() < K){
        cout << -1 << "\n";
        return 0;
    }
    sort(v.begin(), v.end(), greater<int>());
    string s;
    for(int i=0;i<K;i++) s += char('0' + v[i]);
    cout << s << "\n";
    return 0;
}

This editorial was generated by claude5opus-high.

投稿日時:
最終更新: