結果

問題 No.3 ビットすごろく
ユーザー YoshihitoYoshihito
提出日時 2020-04-15 16:54:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 83 ms / 5,000 ms
コード長 1,672 bytes
コンパイル時間 895 ms
コンパイル使用メモリ 150,792 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 01:41:02
合計ジャッジ時間 3,013 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 24 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 15 ms
4,380 KB
testcase_09 AC 39 ms
4,384 KB
testcase_10 AC 56 ms
4,380 KB
testcase_11 AC 33 ms
4,376 KB
testcase_12 AC 22 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 52 ms
4,380 KB
testcase_15 AC 82 ms
4,380 KB
testcase_16 AC 70 ms
4,380 KB
testcase_17 AC 79 ms
4,384 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 82 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 54 ms
4,376 KB
testcase_23 AC 83 ms
4,380 KB
testcase_24 AC 82 ms
4,380 KB
testcase_25 AC 81 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 67 ms
4,376 KB
testcase_29 AC 34 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 29 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, Read};

#[derive(Debug)]
struct Input {
    n: usize,
}

fn next_token(cin_lock: &mut io::StdinLock) -> String {
    cin_lock
        .by_ref()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>()
}

fn read_input(cin_lock: &mut io::StdinLock) -> Input {
    Input {
        n: next_token(cin_lock).parse().unwrap(),
    }
}

fn solve(input: &Input, _cin_lock: &mut io::StdinLock) {
    let mut counts = vec![None; input.n];
    counts[0] = Some(1);

    let mut positions = vec![1];
    while !positions.is_empty() {
        let p = positions.pop().unwrap();
        let next_count = counts[p - 1].unwrap() + 1;
        let bits = p.count_ones() as usize;

        let can_shorten = |c| match c {
            Some(n) => next_count < n,
            None => true,
        };

        if p + bits <= input.n {
            let next_p = p + bits;
            if can_shorten(counts[next_p - 1]) {
                counts[next_p - 1] = Some(next_count);
                positions.push(next_p);
            }
        }

        if p >= 1 + bits {
            let next_p = p - bits;
            if can_shorten(counts[next_p - 1]) {
                counts[next_p - 1] = Some(next_count);
                positions.push(next_p);
            }
        }
    }

    let answer = match counts[input.n - 1] {
        Some(a) => a as i32,
        None => -1,
    };
    println!("{}", answer);
}

fn main() {
    let cin = io::stdin();
    let mut cin_lock = cin.lock();

    let input = read_input(&mut cin_lock);
    solve(&input, &mut cin_lock);
}
0