結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー phsplsphspls
提出日時 2022-10-11 11:15:06
言語 Rust
(1.77.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 1,656 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 150,660 KB
実行使用メモリ 38,664 KB
最終ジャッジ日時 2023-09-07 12:39:16
合計ジャッジ時間 3,885 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 118 ms
38,664 KB
testcase_17 AC 118 ms
38,648 KB
testcase_18 AC 118 ms
38,648 KB
testcase_19 AC 117 ms
38,652 KB
testcase_20 AC 116 ms
38,652 KB
testcase_21 AC 115 ms
38,664 KB
testcase_22 AC 7 ms
7,188 KB
testcase_23 AC 6 ms
7,088 KB
testcase_24 AC 95 ms
32,164 KB
testcase_25 AC 59 ms
34,704 KB
testcase_26 AC 117 ms
38,604 KB
権限があれば一括ダウンロードができます

ソースコード

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(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