結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー phsplsphspls
提出日時 2022-11-02 00:14:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 149,936 KB
実行使用メモリ 39,080 KB
最終ジャッジ日時 2023-09-23 12:07:19
合計ジャッジ時間 4,310 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 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,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,380 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 63 ms
38,936 KB
testcase_17 AC 65 ms
38,932 KB
testcase_18 AC 62 ms
39,076 KB
testcase_19 AC 62 ms
39,080 KB
testcase_20 AC 61 ms
39,044 KB
testcase_21 AC 61 ms
39,036 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 54 ms
31,492 KB
testcase_25 AC 4 ms
4,376 KB
testcase_26 AC 65 ms
38,968 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].to_string();
    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;
    }
    if k.len() > n {
        println!("-1");
        return;
    }
    let maxval = (0..9).rev().map(|i| (i+1).to_string().repeat(c[i])).collect::<Vec<_>>().join("");
    if maxval <= k {
        println!("-1");
        return;
    }
    c.insert(0, 0);
    let k = k.chars().map(|c| c as usize - '0' as usize).collect::<Vec<_>>();
    let mut idx = 0usize;
    let mut stack = vec![];
    while idx < n && c[k[idx]] > 0 {
        stack.push(k[idx]);
        c[k[idx]] -= 1;
        idx += 1;
    }
    if idx == n {
        idx -= 1;
        c[k[idx]] += 1;
        stack.pop();
    }
    loop {
        let mut flg = false;
        for i in k[idx]+1..=9 {
            if c[i] > 0 {
                flg = true;
                c[i] -= 1;
                stack.push(i);
                break;
            }
        }
        if flg {
            break;
        } else {
            idx -= 1;
            c[k[idx]] += 1;
            stack.pop();
        }
    }
    for i in 0..=9 {
        for _ in 0..c[i] {
            stack.push(i);
        }
    }
    let result = stack.iter().map(|i| i.to_string()).collect::<Vec<_>>().join("");
    println!("{}", result);
}
0