結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー phsplsphspls
提出日時 2022-10-04 22:56:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 3,459 ms
コンパイル使用メモリ 158,112 KB
実行使用メモリ 36,860 KB
最終ジャッジ日時 2023-08-28 22:48:57
合計ジャッジ時間 7,519 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 103 ms
36,688 KB
testcase_17 AC 104 ms
36,684 KB
testcase_18 AC 100 ms
36,672 KB
testcase_19 AC 100 ms
36,680 KB
testcase_20 AC 99 ms
36,608 KB
testcase_21 AC 99 ms
36,688 KB
testcase_22 AC 6 ms
5,436 KB
testcase_23 AC 45 ms
32,552 KB
testcase_24 AC 86 ms
30,756 KB
testcase_25 AC 48 ms
32,688 KB
testcase_26 AC 97 ms
36,860 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: value assigned to `idx` is never read
  --> Main.rs:42:21
   |
42 |                     idx += 1;
   |                     ^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

warning: 1 warning emitted

ソースコード

diff #

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(""));
}
0