結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー SSRSSSRS
提出日時 2021-07-30 20:42:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 166,592 KB
実行使用メモリ 5,224 KB
最終ジャッジ日時 2023-10-14 02:58:32
合計ジャッジ時間 3,309 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 20 ms
5,088 KB
testcase_17 AC 22 ms
5,224 KB
testcase_18 AC 14 ms
5,224 KB
testcase_19 AC 14 ms
5,032 KB
testcase_20 AC 34 ms
5,124 KB
testcase_21 AC 34 ms
5,076 KB
testcase_22 AC 35 ms
4,348 KB
testcase_23 AC 11 ms
4,352 KB
testcase_24 AC 21 ms
4,348 KB
testcase_25 AC 13 ms
4,348 KB
testcase_26 AC 14 ms
5,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  string K;
  cin >> N >> K;
  vector<int> c(10);
  c[0] = 0;
  for (int i = 1; i < 10; i++){
    cin >> c[i];
  }
  if (K.size() < N){
    for (int i = 0; i < 10; i++){
      for (int j = 0; j < c[i]; j++){
        cout << i;
      }
    }
    cout << endl;
  } else if (K.size() > N){
    cout << -1 << endl;
  } else {
    for (int i = 0; i < N; i++){
      c[K[i] - '0']--;
    }
    bool ok = false;
    for (int i = N - 1; i >= 0; i--){
      c[K[i] - '0']++;
      for (int j = K[i] - '0' + 1; j < 10; j++){
        c[j]--;
        bool ok2 = true;
        for (int k = 0; k < 10; k++){
          if (c[k] < 0){
            ok2 = false;
          }
        }
        if (ok2){
          string ans;
          for (int k = 0; k < i; k++){
            ans += K[k];
          }
          ans += (char) ('0' + j);
          for (int k = 0; k < 10; k++){
            for (int l = 0; l < c[k]; l++){
              ans += (char) ('0' + k);
            }
          }
          cout << ans << endl;
          ok = true;
          break;
        }
        c[j]++;
      }
      if (ok){
        break;
      }
    }
    if (!ok){
      cout << -1 << endl;
    }
  }
}
0