結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー fukafukatanifukafukatani
提出日時 2021-07-31 11:24:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 3,869 bytes
コンパイル時間 1,246 ms
コンパイル使用メモリ 150,384 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2023-10-14 14:47:26
合計ジャッジ時間 2,958 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 0 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 0 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 1 ms
4,368 KB
testcase_13 AC 0 ms
4,368 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 0 ms
4,372 KB
testcase_16 AC 47 ms
15,188 KB
testcase_17 AC 47 ms
15,220 KB
testcase_18 AC 43 ms
15,124 KB
testcase_19 AC 42 ms
15,204 KB
testcase_20 AC 45 ms
15,232 KB
testcase_21 AC 46 ms
15,228 KB
testcase_22 AC 34 ms
4,368 KB
testcase_23 AC 3 ms
4,372 KB
testcase_24 AC 41 ms
12,648 KB
testcase_25 AC 11 ms
11,296 KB
testcase_26 AC 39 ms
11,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<String>();
    let (n, k) = (v[0].parse::<usize>().unwrap(), v[1].clone());
    let mut c = read_vec::<usize>();

    if k.len() > n {
        println!("-1");
        return;
    }
    if k.len() < n {
        while c.iter().any(|&x| x > 0) {
            for jj in 0..9 {
                if c[jj] >= 1 {
                    c[jj] -= 1;
                    print!("{}", jj + 1);
                    break;
                }
            }
        }
        println!("");
        return;
    }

    let mut k = k
        .chars()
        .map(|x| x.to_digit(10).unwrap() as usize)
        .collect::<Vec<_>>();
    k.reverse();
    let mut kp1 = vec![];
    let mut carry = k[0] == 9;
    if carry {
        kp1.push(0);
    } else {
        kp1.push(k[0] + 1);
    }
    for i in 1..n {
        if carry {
            if k[i] < 9 {
                kp1.push(k[i] + 1);
                carry = false;
            } else {
                kp1.push(0);
            }
        } else {
            kp1.push(k[i]);
        }
    }

    if carry {
        println!("-1");
        return;
    }

    kp1.reverse();
    let mut kp1_count = vec![0; 10];
    for &num in &kp1 {
        kp1_count[num] += 1;
    }
    if (0..9).all(|i| kp1_count[i + 1] == c[i]) {
        for num in kp1 {
            print!("{}", num);
        }
        println!("");
        return;
    }
    debug!(kp1);

    let mut answers = vec![];
    let mut overed = false;
    'outer: for (i, &num) in kp1.iter().enumerate() {
        if overed {
            for j in 0..9 {
                if c[j] >= 1 {
                    c[j] -= 1;
                    answers.push(j + 1);
                    continue 'outer;
                }
            }
        } else {
            let left = if num > 0 { num - 1 } else { 0 };
            for j in left..9 {
                if c[j] >= 1 {
                    c[j] -= 1;
                    answers.push(j + 1);
                    if j + 1 > num {
                        overed = true;
                    }
                    continue 'outer;
                }
            }
            // unreachable!();
            for j in (0..i).rev() {
                c[answers[j] - 1] += 1;
                answers.pop();
                for k in kp1[j]..9 {
                    if c[k] > 0 {
                        answers.push(k + 1);
                        c[k] -= 1;
                        while c.iter().any(|&x| x > 0) {
                            for jj in 0..9 {
                                if c[jj] >= 1 {
                                    c[jj] -= 1;
                                    answers.push(jj + 1);
                                    break;
                                }
                            }
                        }
                        for ans in answers {
                            print!("{}", ans);
                        }
                        println!("");
                        return;
                    }
                }
            }
            println!("-1");
            return;
        }
    }

    for ans in answers {
        print!("{}", ans);
    }
    println!("");
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0