結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー snow39snow39
提出日時 2021-07-30 21:15:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 2,731 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 111,140 KB
実行使用メモリ 40,336 KB
最終ジャッジ日時 2023-10-14 02:51:06
合計ジャッジ時間 3,322 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 67 ms
40,336 KB
testcase_17 AC 66 ms
40,144 KB
testcase_18 AC 74 ms
40,164 KB
testcase_19 AC 74 ms
40,212 KB
testcase_20 AC 67 ms
40,212 KB
testcase_21 AC 67 ms
40,180 KB
testcase_22 AC 66 ms
40,144 KB
testcase_23 AC 3 ms
4,352 KB
testcase_24 AC 49 ms
32,448 KB
testcase_25 AC 55 ms
38,832 KB
testcase_26 AC 63 ms
40,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

const int L = 10;

void solve() {
    int N;
    cin >> N;
    string K;
    cin >> K;
    vector<int> C(L, 0);
    for (int i = 1; i < L; i++) cin >> C[i];

    if (K.size() > N) {
        cout << -1 << endl;
        return;
    }
    string zero(N - K.size(), '0');
    K = zero + K;

    vector<vector<int>> cnt(N + 1, vector<int>(L, 0));
    for (int i = N - 1; i >= 0; i--) {
        for (int j = 1; j < L; j++) {
            if (K[i] - '0' == j)
                cnt[i][j] = cnt[i + 1][j] + 1;
            else
                cnt[i][j] = 0;
        }
    }

    vector<int> rest = C;
    string ans = "";
    bool is_greater = false;
    for (int k = 0; k < N; k++) {
        int digit = -1;
        if (is_greater) {
            for (int i = 1; i < L; i++) {
                if (rest[i] > 0) {
                    digit = i;
                    break;
                }
            }
        } else {
            for (int i = 1; i < L; i++) {
                if (i < K[k] - '0') continue;
                if (rest[i] == 0) continue;
                if (i > K[k] - '0') {
                    digit = i;
                    break;
                }

                int now = k + 1;
                bool will_be_greater = false;
                for (int d = L - 1; d >= 1; d--) {
                    int r = rest[d];
                    if (i == d) r--;
                    if (r == cnt[now][d]) {
                        now += cnt[now][d];
                        continue;
                    } else if (r < cnt[now][d]) {
                        break;
                    } else {
                        will_be_greater = true;
                        break;
                    }
                }
                if (will_be_greater) {
                    digit = i;
                    break;
                }
            }
        }

        if (digit == -1) {
            cout << -1 << endl;
            return;
        }

        ans += char('0' + digit);
        rest[digit]--;
        if (digit > K[k] - '0') is_greater = true;
    }

    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0