結果
| 問題 |
No.1630 Sorting Integers (Greater than K)
|
| コンテスト | |
| ユーザー |
merlin
|
| 提出日時 | 2021-07-30 21:51:33 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,088 bytes |
| コンパイル時間 | 2,974 ms |
| コンパイル使用メモリ | 77,912 KB |
| 実行使用メモリ | 46,268 KB |
| 最終ジャッジ日時 | 2024-09-15 23:50:27 |
| 合計ジャッジ時間 | 6,455 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 9 WA * 13 |
ソースコード
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].toCharArray();
int i,j,c[]=new int[9],m=a.length;
s=bu.readLine().split(" ");
for(i=0;i<9;i++) c[i]=Integer.parseInt(s[i]);
if(m>n) {System.out.print(-1); return;}
StringBuilder ans=new StringBuilder(); boolean pos=true;
if(n>m)
{
for(i=0;i<9;i++)
while(c[i]-->0) ans.append(i+1);
System.out.print(ans);
return;
}
for(i=0;i<a.length;i++)
{
int ch=-1,cur=a[i]-'0'-1;
for(j=0;j<9;j++)
if(c[j]>0 && j>=cur) {ch=j; break;}
if(ch==-1) {pos=false; break;}
ans.append(ch+1);
c[ch]--;
if(ch>cur) break;
}
if(!pos) {System.out.print(-1); return;}
if(ans.length()<n)
{
for(i=0;i<9;i++)
while(c[i]-->0) ans.append(i+1);
System.out.print(ans);
return;
}
char a2[]=ans.toString().toCharArray();
if(!equal(a2,a)) {System.out.print(ans); return;}
Arrays.fill(c,0);
c[a[n-1]-'0'-1]++; int j2=-1;
for(i=n-2;i>=0;i--)
{
int ch=-1,cur=a2[i]-'0'-1;
for(j=0;j<9;j++)
if(c[j]>0 && j>cur) {ch=j; break;}
if(ch!=-1)
{
c[cur]++;
c[ch]--;
a2[i]=(char)(ch+'0'+1);
j2=i+1;
break;
}
}
if(i==-1) {System.out.print(-1); return;}
for(i=0;i<9;i++)
while(c[i]-->0) a2[j2++]=(char)(i+'0'+1);
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;
}
}
merlin