結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー milanis48663220milanis48663220
提出日時 2021-07-30 21:14:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 1,138 ms
コンパイル使用メモリ 120,356 KB
実行使用メモリ 53,368 KB
最終ジャッジ日時 2023-10-14 02:47:27
合計ジャッジ時間 3,026 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 61 ms
53,300 KB
testcase_17 AC 64 ms
53,284 KB
testcase_18 AC 56 ms
53,288 KB
testcase_19 AC 57 ms
53,356 KB
testcase_20 AC 53 ms
53,368 KB
testcase_21 AC 53 ms
53,364 KB
testcase_22 AC 27 ms
7,516 KB
testcase_23 AC 7 ms
7,584 KB
testcase_24 AC 55 ms
42,928 KB
testcase_25 AC 8 ms
9,672 KB
testcase_26 AC 55 ms
53,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

int n, m;
vector<int> c(10);
vector<int> v;
vector<int> get_input(){
    string s; cin >> s;
    int n = s.size();
    vector<int> ans(n);
    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        char c = s[i];
        v[i] = c-'0';
    }
    int carry = 1;
    for(int i = n-1; i >= 0; i--){
        ans[i] = v[i]+carry;
        if(ans[i] == 10){
            ans[i] = 0;
            carry = 1;
        }else{
            carry = 0;
        }
    }
    if(carry == 1){
        vector<int> u(n+1);
        u[0] = 1;
        for(int i = 1; i <= n; i++) u[i] = ans[i-1];
        return u;
    }
    return ans;
}

string ans;
bool dfs(int i, bool big){
    // debug_value(i)
    if(i == m){
        cout << ans << endl;
        return true;
    }
    for(int j = 1; j <= 9; j++){
        if(!big && j < v[i]) continue;
        if(c[j] > 0){
            c[j]--;
            ans += j+'0';
            if(dfs(i+1, big || j > v[i])) return true;
            ans.pop_back();
            c[j]++;
        }
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n;
    v = get_input();
    for(int i = 1; i <= 9; i++) cin >> c[i];
    int k = accumulate(c.begin(), c.end(), 0);
    string ans;
    m = v.size();
    if(k < m){
        cout << -1 << endl;
        return 0;
    }
    if(k > m){
        for(int i = 1; i <= 9; i++){
            for(int j = 0; j < c[i]; j++) cout << i;
        }
        cout << endl;
        return 0;
    }
    if(!dfs(0, false)) cout << -1 << endl;
}
0