結果
問題 | No.1630 Sorting Integers (Greater than K) |
ユーザー |
|
提出日時 | 2021-07-30 22:43:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 1,920 bytes |
コンパイル時間 | 2,503 ms |
コンパイル使用メモリ | 204,788 KB |
最終ジャッジ日時 | 2025-01-23 12:22:53 |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 22 |
ソースコード
#include <bits/stdc++.h>using namespace std;#include <math.h>#include <iomanip>#include <cstdint>#include <string>#include <sstream>template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }#define rep(i,n) for (int i = 0; i < (n); ++i)typedef long long ll;typedef unsigned long long ull;using P=pair<ll,ll>;const int INF=1001001001;const int mod=1e9+7;void solve(){int n;string x;cin>>n>>x;vector<int>s(n);rep(i,n){s[i]=x[i]-'0';}vector<int>cnt(10);string mx;for(int i=1;i<=9;i++){cin>>cnt[i];rep(j,cnt[i]){mx+=(char)('0'+i);}}sort(mx.rbegin(),mx.rend());int X=x.size();if(n<X||mx<=x){cout<<-1<<endl;return;}sort(mx.begin(),mx.end());if(n>X){cout<<mx<<endl;return;}auto c2=cnt;int lst=0;rep(i,n){bool ok=false;for(int j=s[i]+1;j<=9;j++){if(c2[j]!=0){ok=true;}}if(ok){chmax(lst,i);}if(c2[s[i]]==0){break;}c2[s[i]]--;}string ans;rep(i,lst){ans+=x[i];cnt[s[i]]--;}for(int i=s[lst]+1;i<=9;i++){if(cnt[i]!=0){ans+=(char)(i+'0');cnt[i]--;break;}}for(int i=1;i<=9;i++){if(cnt[i]!=0){ans+=string(cnt[i],(char)(i+'0'));}}cout<<ans<<endl;}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);solve();return 0;}