Submission #685092

#TimeUsernameProblemLanguageResultExecution timeMemory
685092KiriLL1caDevil's Share (RMI19_devil)C++17
100 / 100
156 ms5728 KiB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)((x).size())
#define pb push_back

using namespace std;

inline void solve () {
    int k; cin >> k;
    vector <int> cnt (10);
    for (int i = 1; i <= 9; ++i) cin >> cnt[i];
    int n = accumulate(all(cnt), 0);
    string en;

    int dig = 9;
    int iter = k - 1;
    while (iter--) {
        while (!cnt[dig]) --dig;
        en.pb((char)(dig + '0'));
        --cnt[dig];
    }

    while (!cnt[dig]) --dig;
    vector <string> as;
    deque <string> ass;

    while (cnt[dig]) as.pb(string(1, (char)(dig + '0'))), --cnt[dig];

    for (int i = 1; i <= 9; ++i) {
        while (cnt[i]--) {
            ass.pb(string(1, (char)(i + '0')));
        }
    }

    int pos = sz(as) - 1;
    while (sz(ass)) {
        string v = ass.front(); ass.pop_front();
        as[pos] += v;
        if (pos && (!sz(ass) || v != ass.front())) {
            while (sz(as) > pos) {
                ass.push_back(as.back());
                as.pop_back();
            }
        }
        --pos;
        if (!~pos) pos = sz(as) - 1;
    }

    string t;
    for (auto q : as) t += q;
    reverse(all(en)); t += en;
    cout << t << endl;
}

signed main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // LOCAL

    int t = 1; cin >> t;
    while (t--) solve();
    return 0;
}

Compilation message (stderr)

devil.cpp: In function 'void solve()':
devil.cpp:12:9: warning: unused variable 'n' [-Wunused-variable]
   12 |     int n = accumulate(all(cnt), 0);
      |         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...