結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー milanis48663220milanis48663220
提出日時 2021-07-30 21:11:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,188 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 119,560 KB
実行使用メモリ 45,788 KB
最終ジャッジ日時 2023-10-14 02:34:05
合計ジャッジ時間 3,352 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 49 ms
45,660 KB
testcase_19 WA -
testcase_20 AC 42 ms
45,600 KB
testcase_21 AC 44 ms
45,692 KB
testcase_22 WA -
testcase_23 AC 6 ms
7,488 KB
testcase_24 WA -
testcase_25 AC 8 ms
9,596 KB
testcase_26 AC 46 ms
45,628 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘bool dfs(int, bool)’ 内:
main.cpp:71:1: 警告: 制御が非 void 関数の終りに到達しました [-Wreturn-type]
   71 | }
      | ^

ソースコード

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]++;
        }
    }
}

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 = 9; i >= 1; i--){
            for(int j = 0; j < c[i]; j++) cout << i;
        }
        cout << endl;
        return 0;
    }
    if(!dfs(0, false)) cout << -1 << endl;
}
0