結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー merlinmerlin
提出日時 2021-07-30 22:45:54
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,381 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 74,644 KB
実行使用メモリ 56,540 KB
最終ジャッジ日時 2023-10-14 07:01:20
合計ジャッジ時間 7,089 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,384 KB
testcase_01 AC 46 ms
49,452 KB
testcase_02 AC 45 ms
49,428 KB
testcase_03 AC 47 ms
49,552 KB
testcase_04 AC 47 ms
49,364 KB
testcase_05 AC 46 ms
49,548 KB
testcase_06 AC 46 ms
49,480 KB
testcase_07 AC 46 ms
49,568 KB
testcase_08 AC 47 ms
49,516 KB
testcase_09 RE -
testcase_10 AC 46 ms
49,852 KB
testcase_11 AC 47 ms
49,660 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 WA -
testcase_15 AC 47 ms
49,404 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 197 ms
55,708 KB
testcase_19 RE -
testcase_20 AC 154 ms
55,144 KB
testcase_21 AC 148 ms
54,560 KB
testcase_22 AC 184 ms
56,016 KB
testcase_23 AC 101 ms
53,928 KB
testcase_24 AC 195 ms
55,900 KB
testcase_25 AC 179 ms
55,728 KB
testcase_26 AC 203 ms
55,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]); char a[]=s[1].trim().toCharArray();
        int i,j,c[]=new int[10],m=a.length;
        s=bu.readLine().split(" ");
        for(i=0;i<9;i++) c[i+1]=Integer.parseInt(s[i]);

        if(m>n) {System.out.print(-1); return;}
        if(n>m)
        {
            StringBuilder ans=new StringBuilder();
            for(i=1;i<10;i++)
            while(c[i]-->0) ans.append(i);
            System.out.print(ans);
            return;
        }

        char a2[]=new char[n]; boolean pos=true;
        for(i=0;i<m;i++)
        {
            int ch=-1,cur=a[i]-'0';
            for(j=1;j<10;j++)
            if(c[j]>0 && j>=cur) {ch=j; break;}
            if(ch==-1)
            {
                int min=0+'0';
                for(j=1;j<10;j++)
                if(c[j]>0) min=j+'0';

                for(j=i-1;j>=0;j--)
                if(a2[j]<min) break;
                else min=Math.max(min,a2[j]);
                if(j==-1) {pos=false; break;}

                min=j;
                for(j=i-1;j>min;j--)
                c[a2[j]-'0']++;

                cur=a2[min]-'0';
                for(j=1;j<10;j++)
                if(j>cur && c[j]>0) {ch=j; break;}
            }

            a2[i]=(char)(ch+'0');
            c[ch]--;
            if(ch>cur) break;
        }
        if(!pos) {System.out.print(-1); return;}

        if(i<n)
        {
            j=i+1;
            for(i=1;i<10;i++)
            while(c[i]-->0) a2[j++]=(char)('0'+i);
        }
        if(!equal(a2,a)) {System.out.print(new String(a2)); return;}

        for(i=n-2;i>=0;i--)
        if(a2[i]<a2[i+1]) break;
        if(i==-1) {System.out.print(-1); return;}

        //System.out.println(i);
        j=i; int min=i+1;
        for(i=j+1;i<n;i++)
        if(a2[i]>a2[j] && a2[i]<a2[min]) min=i;

        //System.out.println(j+" "+min);
        char t=a2[min];
        a2[min]=a2[j];
        a2[j]=t;
        Arrays.sort(a2,j+1,n);
        System.out.print(new String(a2));
    }

    static boolean equal(char a[],char b[])
    {
        for(int i=0;i<a.length;i++)
        if(a[i]!=b[i]) return false;
        return true;
    }
}
0