結果

問題 No.3 ビットすごろく
コンテスト
ユーザー tsubu_taiyaki
提出日時 2017-01-30 09:54:11
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,547 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,320 ms
コンパイル使用メモリ 141,124 KB
最終ジャッジ日時 2026-05-10 04:35:32
合計ジャッジ時間 7,066 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
warning: unused import: `std::cmp::*`
 --> src/main.rs:4:5
  |
4 | use std::cmp::*;
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

error[E0782]: expected a type, found a trait
 --> src/main.rs:7:21
  |
7 |     tokens: &'a mut Iterator<Item = String>,
  |                     ^^^^^^^^^^^^^^^^^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
7 |     tokens: &'a mut dyn Iterator<Item = String>,
  |                     +++

error[E0782]: expected a type, found a trait
  --> src/main.rs:11:23
   |
11 |     fn new(i: &'a mut Iterator<Item = String>) -> Self {
   |                       ^^^^^^^^^^^^^^^^^^^^^^^
   |
help: use a new generic type parameter, constrained by `Iterator<Item = String>`
   |
11 -     fn new(i: &'a mut Iterator<Item = String>) -> Self {
11 +     fn new<T: Iterator<Item = String>>(i: &'a mut T) -> Self {
   |
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
   |
11 |     fn new(i: &'a mut impl Iterator<Item = String>) -> Self {
   |                       ++++
help: alternatively, use a trait object to accept any type that implements `Iterator<Item = String>`, accessing its methods at runtime using dynamic dispatch
   |
11 |     fn new(i: &'a mut dyn Iterator<Item = String>) -> Self {
   |                       +++

error[E0282]: type annotations needed
  --> src/main.rs:22:34
   |
22 |         self.tokens.take(n).map(|s| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
   |                                  ^        - type must be known at this point
   |
help: consider giving this closure parameter an explicit type
   |
22 |         self.tokens.take(n).map(|s: /* Type */| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
   |                                   ++++++++++++

Some errors have detailed explanations: E0282, E0782.
For m

ソースコード

diff #
raw source code

use std::io::{self, BufRead};
use std::str::FromStr;
use std::collections::*;
use std::cmp::*;

struct Parser<'a> {
    tokens: &'a mut Iterator<Item = String>,
}

impl<'a> Parser<'a> {
    fn new(i: &'a mut Iterator<Item = String>) -> Self {
        Parser {tokens: i}
    }
    fn take<T: FromStr>(&mut self) -> T {
        match self.tokens.next().expect("empty iterator").parse() {
            Ok(x) => x,
            Err(_) => panic!()
        }
    }

    fn take_some<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        self.tokens.take(n).map(|s| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
    }
}

fn bit_count(n: i64) -> i64 {
    match n {
        0 => 0,
        _ => 1 + bit_count(n - (n&(-n))),
    }
}

fn main() {
    let stdin = io::stdin();
    let mut tokens = stdin.lock().lines().filter_map(|x| x.ok()).flat_map(|x| x.split_whitespace().map(|s| s.to_owned()).collect::<Vec<String>>());
    let mut parser = Parser::new(&mut tokens);

    let n: i64 = parser.take();

    let mut v = Vec::new();
    v.resize((n+1) as usize, None::<i64>);
    let mut q = BinaryHeap::new();
    q.push((-1i64,1i64));

    while let Some((c, p)) = q.pop() {
        if p <= 0 {
            continue;
        }
        if p > n {
            continue;
        }
        if v[p as usize].is_some() {
            continue;
        }

        v[p as usize] = Some(c);
        q.push((c-1, p + bit_count(p)));
        q.push((c-1, p - bit_count(p)));
    }
    println!("{}", match v[n as usize] { Some(c) => -c, None => -1});
}
0