結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー milanis48663220
提出日時 2021-07-30 21:14:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,206 bytes
コンパイル時間 1,223 ms
コンパイル使用メモリ 117,476 KB
最終ジャッジ日時 2025-01-23 11:00:34
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
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; }
using namespace std;
typedef long long ll;
int n, m;
vector<int> c(10);
vector<int> v;
vector<int> get_input(){
string s; cin >> s;
int n = s.size();
vector<int> ans(n);
vector<int> v(n);
for(int i = 0; i < n; i++) {
char c = s[i];
v[i] = c-'0';
}
int carry = 1;
for(int i = n-1; i >= 0; i--){
ans[i] = v[i]+carry;
if(ans[i] == 10){
ans[i] = 0;
carry = 1;
}else{
carry = 0;
}
}
if(carry == 1){
vector<int> u(n+1);
u[0] = 1;
for(int i = 1; i <= n; i++) u[i] = ans[i-1];
return u;
}
return ans;
}
string ans;
bool dfs(int i, bool big){
// debug_value(i)
if(i == m){
cout << ans << endl;
return true;
}
for(int j = 1; j <= 9; j++){
if(!big && j < v[i]) continue;
if(c[j] > 0){
c[j]--;
ans += j+'0';
if(dfs(i+1, big || j > v[i])) return true;
ans.pop_back();
c[j]++;
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> n;
v = get_input();
for(int i = 1; i <= 9; i++) cin >> c[i];
int k = accumulate(c.begin(), c.end(), 0);
string ans;
m = v.size();
if(k < m){
cout << -1 << endl;
return 0;
}
if(k > m){
for(int i = 1; i <= 9; i++){
for(int j = 0; j < c[i]; j++) cout << i;
}
cout << endl;
return 0;
}
if(!dfs(0, false)) cout << -1 << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0