結果
| 問題 |
No.1630 Sorting Integers (Greater than K)
|
| コンテスト | |
| ユーザー |
momoyuu
|
| 提出日時 | 2024-10-06 19:50:19 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 46 ms / 2,000 ms |
| コード長 | 1,278 bytes |
| コンパイル時間 | 1,123 ms |
| コンパイル使用メモリ | 97,248 KB |
| 実行使用メモリ | 43,440 KB |
| 最終ジャッジ日時 | 2024-10-06 19:50:22 |
| 合計ジャッジ時間 | 2,894 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 22 |
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n;
cin>>n;
string k;
cin>>k;
vector<int> c(10,0);
for(int i = 1;i<=9;i++) cin>>c[i];
if(n!=k.size()){
int m = k.size();
if(m>n) {
cout<<-1<<endl;
return 0;
}
string ans = "";
for(int i = 1;i<=9;i++) for(int j = 0;j<c[i];j++) ans += '0' + i;
cout<<ans<<endl;
return 0;
}
string ans = "";
auto dfs = [&](auto dfs,int ni) -> bool {
if(ni==k.size()) return true;
int p = k[ni] - '0';
if(c[p]!=0&&ni+1!=k.size()){
c[p]--;
ans += k[ni];
bool ok = dfs(dfs,ni+1);
if(ok) return true;
c[p]++;
ans.pop_back();
}
for(int i = p+1;i<=9;i++){
if(c[i]==0) continue;
ans += '0' + i;
c[i]--;
for(int j = 1;j<=9;j++){
for(int l = 0;l<c[j];l++) ans += '0' + j;
}
return true;
}
return false;
};
dfs(dfs,0);
if(ans.empty()) cout<<-1<<endl;
else cout<<ans<<endl;
}
momoyuu