結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー phspls
提出日時 2022-10-11 11:13:31
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,659 bytes
コンパイル時間 13,556 ms
コンパイル使用メモリ 377,608 KB
実行使用メモリ 38,824 KB
最終ジャッジ日時 2024-06-25 06:41:51
合計ジャッジ時間 15,918 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 2
other AC * 4 WA * 18
権限があれば一括ダウンロードができます

ソースコード

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().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();

    c.insert(0, 0);
    if k.len() < n {
        println!("{}", (0..10).map(|i| i.to_string().repeat(c[i])).collect::<Vec<_>>().join(""));
        return;
    }
    let maxval = (0..10).rev().map(|i| i.to_string().repeat(c[i])).collect::<Vec<_>>().join("");
    if k.len() > n || k.iter().map(|&c| c.to_string()).collect::<Vec<_>>().join("") >= maxval {
        println!("-1");
        return;
    }
    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 {
        idx -= 1;
        c[result.pop().unwrap()] += 1;
    }
    loop {
        let mut flg = false;
        for i in k[idx]+1..10 {
            if c[i] > 0 {
                result.push(c[i]);
                idx += 1;
                c[i] -= 1;
                flg = true;
                break;
            }
        }
        if flg {
            break;
        } else {
            c[result.pop().unwrap()] += 1;
            idx -= 1;
        }
    }
    for i in 0..10 {
        for _ in 0..c[i] { result.push(i); }
    }
    println!("{}", result.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(""));
}
0