結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー karinohitokarinohito
提出日時 2021-07-16 12:09:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 1,821 bytes
コンパイル時間 2,200 ms
コンパイル使用メモリ 76,124 KB
実行使用メモリ 60,260 KB
最終ジャッジ日時 2023-10-14 02:22:13
合計ジャッジ時間 9,154 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,820 KB
testcase_01 AC 126 ms
56,088 KB
testcase_02 AC 126 ms
55,416 KB
testcase_03 AC 125 ms
55,740 KB
testcase_04 AC 126 ms
55,620 KB
testcase_05 AC 126 ms
56,000 KB
testcase_06 AC 127 ms
56,128 KB
testcase_07 AC 126 ms
55,868 KB
testcase_08 AC 126 ms
55,684 KB
testcase_09 AC 126 ms
55,504 KB
testcase_10 AC 130 ms
55,644 KB
testcase_11 AC 127 ms
55,920 KB
testcase_12 AC 127 ms
55,672 KB
testcase_13 AC 126 ms
55,820 KB
testcase_14 AC 128 ms
55,420 KB
testcase_15 AC 128 ms
55,664 KB
testcase_16 AC 340 ms
59,516 KB
testcase_17 AC 356 ms
59,548 KB
testcase_18 AC 312 ms
59,812 KB
testcase_19 AC 311 ms
60,168 KB
testcase_20 AC 290 ms
60,252 KB
testcase_21 AC 290 ms
59,864 KB
testcase_22 AC 288 ms
60,260 KB
testcase_23 AC 228 ms
58,560 KB
testcase_24 AC 304 ms
59,952 KB
testcase_25 AC 252 ms
58,384 KB
testcase_26 AC 283 ms
59,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
    
      	Scanner sc = new Scanner(System.in);
      	int N=sc.nextInt();
      	int bign=-1;
      	int lc=-1;
		String K = sc.next();
      	int[] Cjust=new int[9];
     	for(var i=0;i<9;i++){
          	Cjust[i]=sc.nextInt();
        }
      	int[] Cbig=new int[9];
      	for (int i=0;i<9;i++){
        	Cbig[i]=Cjust[i];
        }
		sc.close();

      	if(N<(int)K.length()){
          System.out.println(-1);
        }
      	else if(N>(int)K.length()){
          StringBuilder sb = new StringBuilder();
          for(int i=0;i<9;i++){
          	for(int num=Cjust[i];num>0;num--){
              	sb.append((char)(i+'1')); 
            }
          }
          System.out.println(sb);
        }
      	else{
          for (int i=0;i<N;i++){
            char Ki=K.charAt(i);
            for (int c=(int)(Ki-'1')+1;c<9;c++){
            	if(Cjust[c]>0){
                  for(int d=Math.max(bign,0);d<i;d++){
                  	Cbig[(int)K.charAt(d)-'1']--;
                  }
                  bign=i;
                  lc=c;
                  break;
                }
            }
            if(K.charAt(i)!='0'&&Cjust[K.charAt(i)-'1']>0){
              Cjust[K.charAt(i)-'1']--;
            }
            else break;
        }
        if(bign==-1){
          System.out.println(-1); 
        }
        else{
          StringBuilder sb = new StringBuilder();
          sb.append(K.substring(0,bign)).append((char)(lc+'1'));
          
         
          Cbig[lc]--;
          for(int j=0;j<9;j++){
            for(int k=0;k<Cbig[j];k++){
              sb.append((char)(j+'1'));
            }
          }
          System.out.println(sb);
        } 
        }
	}
}
0