結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー monnumonnu
提出日時 2021-07-30 21:11:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,437 bytes
コンパイル時間 3,936 ms
コンパイル使用メモリ 229,016 KB
実行使用メモリ 38,860 KB
最終ジャッジ日時 2023-10-14 02:38:06
合計ジャッジ時間 6,088 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 78 ms
38,860 KB
testcase_17 AC 83 ms
38,648 KB
testcase_18 AC 67 ms
38,784 KB
testcase_19 AC 66 ms
38,860 KB
testcase_20 AC 85 ms
38,860 KB
testcase_21 AC 86 ms
38,840 KB
testcase_22 AC 36 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 73 ms
31,484 KB
testcase_25 AC 65 ms
38,704 KB
testcase_26 AC 67 ms
38,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
using Graph=vector<vector<pair<int,int>>>;
#define MAX 100000
#define MOD 1000000007
#define INF 1000000000

int main(){
  int N;
  string K;
  cin>>N>>K;
  vector<int> c(9);
  for(int i=0;i<9;i++){
    cin>>c[i];
  }
  int n=K.size();
  if(n>N){
    cout<<-1<<endl;
  }else if(n<N){
    for(int i=0;i<9;i++){
      for(int j=0;j<c[i];j++){
        cout<<i+1;
      }
    }
    cout<<endl;
  }else{
    vector<vector<int>> sum(N+1,vector<int>(9,0));
    for(int i=0;i<N;i++){
      for(int j=0;j<9;j++){
        sum[i+1][j]=sum[i][j];
      }
      if(K[i]!='0'){
        sum[i+1][K[i]-'1']++;
      }
    }
    int i=0;
    while(i<n-1&&K[i]!='0'){
      i++;
    }
    for(;i>=0;i--){
      bool flag=true;
      for(int j=0;j<9;j++){
        if(c[j]<sum[i][j]){
          flag=false;
          break;
        }
      }
      if(!flag){
        continue;
      }
      int k=-1;
      for(int j=K[i]-'0';j<9;j++){
        if(c[j]-sum[i][j]>0){
          k=j;
          flag=false;
          break;
        }
      }
      if(flag){
        continue;
      }
      for(int j=0;j<i;j++){
        cout<<K[j];
      }
      cout<<k+1;
      c[k]--;
      for(int j=0;j<9;j++){
        for(int l=0;l<c[j]-sum[i][j];l++){
          cout<<j+1;
        }
      }
      cout<<endl;
      return 0;
    }
    cout<<-1<<endl;
  }
}
0