結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-05-04 22:35:00 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 3,357 bytes |
| コンパイル時間 | 13,167 ms |
| コンパイル使用メモリ | 383,920 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 08:59:06 |
| 合計ジャッジ時間 | 14,474 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#![allow(unused_imports)]
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]
use std::{cmp, fmt};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, Write};
use std::ops::{Add, Div, Mul, Sub};
use std::str::{self, FromStr};
use std::iter::FromIterator as _FromIterator;
fn main() {
let n: usize = {
let mut sc = InputScanOnce::new(io::stdin(), 1024);
sc.next()
};
let mut is = BTreeSet::from_iter(vec![0]);
let mut s = vec![None; n];
s[0] = Some(1);
loop {
if let Some(l) = s[(n - 1) as usize] {
println!("{}", l);
return;
}
if is.is_empty() {
println!("-1");
return;
}
let mut is2 = BTreeSet::new();
for &i in &is {
let l = s[i].unwrap();
let d = (i + 1).count_ones() as usize;
if d <= i && (s[i - d].is_none() || s[i - d].is_some() && s[i - d].unwrap() > l + 1) {
s[i - d] = Some(l + 1);
is2.insert(i - d);
}
if i + d < n && (s[i + d].is_none() || s[i + d].is_some() && s[i - d].unwrap() > l + 1)
{
s[i + d] = Some(l + 1);
is2.insert(i + d);
}
}
is = is2;
}
}
struct InputScanOnce {
buf: Vec<u8>,
pos: usize,
}
#[allow(dead_code)]
impl InputScanOnce {
fn new<R: Read>(mut reader: R, estimated: usize) -> Self {
let mut buf = Vec::with_capacity(estimated);
let _ = io::copy(&mut reader, &mut buf).unwrap();
InputScanOnce { buf: buf, pos: 0 }
}
#[inline]
fn next<T: FromStr>(&mut self) -> T
where
T::Err: fmt::Debug,
{
let mut start = None;
loop {
match (self.buf[self.pos], start.is_some()) {
(b' ', true) | (b'\n', true) => break,
(_, true) | (b' ', false) | (b'\n', false) => self.pos += 1,
(_, false) => start = Some(self.pos),
}
}
let target = &self.buf[start.unwrap()..self.pos];
unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
}
fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
where
T::Err: fmt::Debug,
{
(0..n).map(|_| self.next()).collect()
}
fn pairs<T1: FromStr, T2: FromStr>(&mut self, n: usize) -> Vec<(T1, T2)>
where
T1::Err: fmt::Debug,
T2::Err: fmt::Debug,
{
(0..n).map(|_| (self.next(), self.next())).collect()
}
fn trios<T1: FromStr, T2: FromStr, T3: FromStr>(&mut self, n: usize) -> Vec<(T1, T2, T3)>
where
T1::Err: fmt::Debug,
T2::Err: fmt::Debug,
T3::Err: fmt::Debug,
{
(0..n)
.map(|_| (self.next(), self.next(), self.next()))
.collect()
}
fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
where
T::Err: fmt::Debug,
{
(0..m).map(|_| self.vec(n)).collect()
}
fn strings_as_mat<T, F: FnMut(u8) -> T>(&mut self, h: usize, mut f: F) -> Vec<Vec<T>> {
(0..h)
.map(|_| {
let l = self.next::<String>();
l.as_bytes().iter().cloned().map(&mut f).collect()
})
.collect()
}
}