結果
| 問題 |
No.1630 Sorting Integers (Greater than K)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-30 20:46:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 54 ms / 2,000 ms |
| コード長 | 699 bytes |
| コンパイル時間 | 762 ms |
| コンパイル使用メモリ | 67,576 KB |
| 実行使用メモリ | 44,332 KB |
| 最終ジャッジ日時 | 2024-09-15 22:38:25 |
| 合計ジャッジ時間 | 2,217 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 22 |
コンパイルメッセージ
main.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main()
| ^~~~
ソースコード
#include<iostream>
#include<algorithm>
using namespace std;
int N;
string K,ans;
int C[10];
bool dfs(int id)
{
if(id==N)return false;
int c=K[id]-'0';
if(C[c]>0)
{
C[c]--;
if(dfs(id+1))
{
ans+=c+'0';
return true;
}
C[c]++;
}
c++;
while(c<=9&&C[c]==0)c++;
if(c==10)return false;
C[c]--;
for(int i=9;i>=1;i--)for(int j=0;j<C[i];j++)ans+=i+'0';
ans+=c+'0';
return true;
}
main()
{
cin>>N>>K;
for(int i=1;i<=9;i++)cin>>C[i];
if(K.size()!=N)
{
if(K.size()>N)cout<<-1<<endl;
else
{
for(int i=1;i<=9;i++)for(int j=0;j<C[i];j++)cout<<i;
cout<<endl;
}
return 0;
}
if(!dfs(0))cout<<-1<<endl;
else
{
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}
}