結果
| 問題 |
No.1630 Sorting Integers (Greater than K)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-04 22:56:04 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 120 ms / 2,000 ms |
| コード長 | 1,782 bytes |
| コンパイル時間 | 24,494 ms |
| コンパイル使用メモリ | 386,140 KB |
| 実行使用メモリ | 36,852 KB |
| 最終ジャッジ日時 | 2024-12-30 13:18:55 |
| 合計ジャッジ時間 | 27,790 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 22 |
コンパイルメッセージ
warning: value assigned to `idx` is never read --> src/main.rs:42:21 | 42 | idx += 1; | ^^^ | = help: maybe it is overwritten before being read? = note: `#[warn(unused_assignments)]` on by default
ソースコード
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().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("");
let kstr = k.iter().map(|&c| c.to_string()).collect::<Vec<_>>().join("");
if kstr.len() > maxval.len() || maxval <= kstr {
println!("-1");
return;
}
c.insert(0, 0);
let mut result = vec![];
let mut idx = 0;
while idx < n && c[k[idx] as usize - '0' as usize] > 0 {
let i = k[idx] as usize - '0' as usize;
result.push(i);
c[i] -= 1;
idx += 1;
}
if idx == n {
c[result.pop().unwrap()] += 1;
idx -= 1;
}
loop {
let start = k[idx] as usize - '0' as usize;
if (start+1..=9).any(|i| c[i] > 0) {
for i in start+1..=9 {
if c[i] > 0 {
result.push(i);
c[i] -= 1;
idx += 1;
break;
}
}
break;
} else {
c[result.pop().unwrap()] += 1;
idx -= 1;
}
}
(0..10).for_each(|i| { (0..c[i]).for_each(|_| { result.push(i); }); });
println!("{}", result.into_iter().map(|i| i.to_string()).collect::<Vec<_>>().join(""));
}