公式

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

Gemini 3.8 Flash (High)

Overview

This problem asks us to find the maximum integer that can be formed by choosing exactly \(K\) cards with numbers greater than or equal to \(X\) and rearranging them (output \(-1\) if there are fewer than \(K\) cards satisfying the condition).

Analysis

Key Observations

  1. The number of digits is always \(K\)
    The numbers written on the cards are all single-digit integers between \(1\) and \(9\), inclusive. Therefore, any integer formed by concatenating \(K\) cards will always have \(K\) digits (there is no leading zero to reduce the number of digits).

  2. Placing larger digits on the left is optimal (Greedy Approach)
    To maximize an integer with a fixed number of digits, it is optimal to place the largest possible digits (\(9, 8, \dots\)) in the higher place values (the leftmost positions). Therefore, we can achieve the maximum value by prioritizing larger numbers when selecting the \(K\) cards and arranging them from left to right in descending order.

Naive Approach and Optimization

  • Naive Approach:
    One could collect all cards with values greater than or equal to \(X\) into an array, sort them in descending order, and concatenate the first \(K\) elements. However, since \(N \le 10^6\), sorting the entire array takes \(O(N \log N)\) time.
  • Optimized Approach:
    The numbers on the cards are restricted to only \(9\) distinct values, from \(1\) to \(9\). Thus, instead of sorting the array, we can count the frequency of each digit (the idea behind bucket sort) to solve the problem in \(O(N)\) time.

Algorithm

  1. Count Frequencies:
    Prepare an array cnt of size \(10\) and count the occurrences of each card’s number \(A_i\) from the input.
  2. Feasibility Check:
    If the total count of cards with numbers between \(X\) and \(9\) is less than \(K\), it is impossible to form a valid integer, so output -1 and terminate.
  3. Construct the Answer:
    Iterate through the digits from \(9\) down to \(X\) in descending order. For each digit \(d\), use \(\min(rem, cnt[d])\) cards, where \(rem\) is the remaining number of cards needed (initially \(K\)). Append that many instances of digit \(d\) to a string. Once \(K\) cards have been used, terminate and output the string.

Complexity

  • Time Complexity: \(O(N + D)\) (where \(D\) is the number of distinct digits, \(D \le 10\))
    Counting the cards takes \(O(N)\) time, and constructing the answer takes \(O(D + K)\) time. This runs well within the time limit for \(N \le 10^6\).
  • Space Complexity: \(O(D + K)\)
    It uses \(O(D)\) memory for the frequency array and \(O(K)\) memory to store the output string. Since \(D \le 10\) and \(K \le 18\), this can be considered \(O(1)\).

Implementation Notes

  • Fast I/O:
    Since \(N\) can be as large as \(10^6\), use cin.tie(NULL); ios_base::sync_with_stdio(false); in C++ for faster input/output.

  • Constructing as a String:
    Although the result fits into a 64-bit integer type (long long) since \(K \le 18\), appending characters to a string allows for a clean and simple implementation without worrying about integer overflow.

    Source Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, x;
    if (!(cin >> n >> k >> x)) return 0;

    vector<int> cnt(10, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        cnt[a]++;
    }

    int valid_count = 0;
    for (int d = x; d <= 9; ++d) {
        valid_count += cnt[d];
    }

    if (valid_count < k) {
        cout << -1 << "\n";
        return 0;
    }

    string ans = "";
    int rem = k;
    for (int d = 9; d >= x; --d) {
        int take = min(rem, cnt[d]);
        ans.append(take, '0' + d);
        rem -= take;
        if (rem == 0) break;
    }

    cout << ans << "\n";

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

投稿日時:
最終更新: