Approach
Sorting and greedy selection
For ABC416 C — Concat (X-th), the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 46 lines of C++ from the credited upstream file abc416_c.cpp.
- The implementation visibly relies on sequence storage.
- 3 loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#include <algorithm>2#include <functional>3#include <iostream>4#include <vector>5 6using namespace std;7 8using ui = unsigned int;9 10int main() {11 ui n, k, x;12 cin >> n >> k >> x;13 14 vector<string> s(n);15 for (ui i = 0; i < n; i++) {16 cin >> s[i];17 }18 19 vector<string> results;20 21 function<void(vector<int>&, int)> generate = [&](vector<int>& seq, ui pos) {22 if (pos == k) {23 string result = "";24 for (ui i = 0; i < k; i++) {25 result += s[seq[i]];26 }27 results.push_back(result);28 return;29 }30 31 for (ui i = 0; i < n; i++) {32 seq[pos] = i;33 generate(seq, pos + 1);34 }35 };36 37 vector<int> seq(k);38 generate(seq, 0);39 40 sort(results.begin(), results.end());41 42 cout << results[x - 1] << endl;43 44 return 0;45}46