/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 15ms 644.0 KiB
#3 Accepted 15ms 668.0 KiB
#4 Accepted 18ms 692.0 KiB
#5 Accepted 15ms 580.0 KiB
#6 Accepted 15ms 580.0 KiB
#7 Wrong Answer 17ms 576.0 KiB
#8 Wrong Answer 3ms 580.0 KiB
#9 Wrong Answer 2ms 324.0 KiB
#10 Wrong Answer 15ms 904.0 KiB
#11 Wrong Answer 2ms 324.0 KiB
#12 Wrong Answer 4ms 576.0 KiB
#13 Wrong Answer 9ms 680.0 KiB
#14 Wrong Answer 7ms 616.0 KiB
#15 Wrong Answer 14ms 676.0 KiB

Code

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

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

// Function to compare two large numbers as strings by splitting
bool compare_as_large_integers(const string &a, const string &b) {
    if (a.length() != b.length()) {
        return a.length() > b.length();
    }
    return a > b;  // Lexicographical comparison if lengths are equal
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<string> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    while (k > 0 && arr.size() > 1) {
        string max_concat = "";
        int max_index = 0;
        for (int j = 0; j < arr.size() - 1; j++) {
            string concat = arr[j] + arr[j + 1];  
            if (compare_as_large_integers(concat, max_concat)) {
                max_concat = concat;
                max_index = j;
            }
        }

        arr[max_index] = max_concat; 
        arr.erase(arr.begin() + max_index + 1); 
        k--;
    }

    // Find the maximum string in the remaining list
    string ans = arr[0];
    for (int i = 1; i < arr.size(); i++) {
        if (compare_as_large_integers(arr[i], ans)) {
            ans = arr[i];
        }
    }

    cout << ans << endl;
}

int main() {
    fastio;
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1083 Number concatenation
Contest
Bangladesh 2.0
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-16 16:41:54
Judged At
2024-10-03 13:26:14
Judged By
Score
30
Total Time
18ms
Peak Memory
904.0 KiB