結果
| 問題 |
No.1630 Sorting Integers (Greater than K)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-05 23:14:26 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 113 ms / 2,000 ms |
| コード長 | 1,656 bytes |
| コンパイル時間 | 13,256 ms |
| コンパイル使用メモリ | 400,644 KB |
| 実行使用メモリ | 38,824 KB |
| 最終ジャッジ日時 | 2025-01-02 02:25:32 |
| 合計ジャッジ時間 | 13,534 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 22 |
ソースコード
fn main() {
let mut nk = String::new();
std::io::stdin().read_line(&mut nk).ok();
let nk: Vec<&str> = nk.trim().split_whitespace().collect();
let n = nk[0].parse::<usize>().unwrap();
let k = nk[1].chars().map(|c| c as usize - '0' as usize).collect::<Vec<_>>();
let mut c = String::new();
std::io::stdin().read_line(&mut c).ok();
let mut c: Vec<usize> = c.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
if k.len() < n {
println!("{}", (0..9).map(|i| (i+1).to_string().repeat(c[i])).collect::<Vec<_>>().join(""));
return;
}
let maxval = (0..9).rev().map(|i| (i+1).to_string().repeat(c[i])).collect::<Vec<_>>().join("");
if k.len() > n || maxval <= k.iter().map(|i| i.to_string()).collect::<Vec<_>>().join("") {
println!("-1");
return;
}
c.insert(0, 0);
let mut result = vec![];
let mut idx = 0usize;
while idx < n && c[k[idx]] > 0 {
result.push(k[idx]);
c[k[idx]] -= 1;
idx += 1;
}
if idx == n {
c[result.pop().unwrap()] += 1;
idx -= 1;
}
loop {
let mut flg = false;
for i in k[idx]+1..10 {
if c[i] > 0 {
result.push(i);
c[i] -= 1;
idx += 1;
flg = true;
break;
}
}
if flg {
break;
} else {
c[result.pop().unwrap()] += 1;
idx -= 1;
}
}
(0..10).for_each(|i| (0..c[i]).for_each(|_| result.push(i)) );
println!("{}", result.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(""));
}