Submission #64070272


Source Code Expand

Copy
//#[derive_readable]
#[derive(Debug, Clone)]
struct Problem {
n: usize,
pos: Pos,
dirs: Vec<char>,
}
impl Problem {
fn read() -> Problem {
input! {
n: usize,
(r, c) :(i64, i64),
dirs: Chars,
}
let pos = Pos::new(r, c);
Problem { n, pos, dirs }
}
fn solve(&self) -> Answer {
let n = self.n;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//#[derive_readable]
#[derive(Debug, Clone)]
struct Problem {
    n: usize,
    pos: Pos,
    dirs: Vec<char>,
}

impl Problem {
    fn read() -> Problem {
        input! {
            n: usize,
            (r, c) :(i64, i64),
            dirs: Chars,
        }
        let pos = Pos::new(r, c);
        Problem { n, pos, dirs }
    }

    fn solve(&self) -> Answer {
        let n = self.n;
        let pos = self.pos;
        let dirs = self
            .dirs
            .iter()
            .copied()
            .map(|ch| {
                if ch == 'N' {
                    Pos::new(-1, 0)
                } else if ch == 'W' {
                    Pos::new(0, -1)
                } else if ch == 'S' {
                    Pos::new(1, 0)
                } else {
                    // ch == 'E'
                    Pos::new(0, 1)
                }
            })
            .collect_vec();

        let prefix_dirs_sum = {
            let mut tmp = vec![Pos::new(0, 0); n + 1];
            for i in 0..n {
                tmp[i + 1] = tmp[i] + dirs[i]
            }

            tmp
        };

        let mut prefix_sum_set: HashSet<Pos> = HashSet::new();
        prefix_sum_set.insert(prefix_dirs_sum[0]);

        let mut ans = vec![];

        for t in 1..=n {
            // prefix_dirs_sum[t] - prefix_dirs_sum[l]  == pos となるような l が存在するか
            let target = prefix_dirs_sum[t] - pos;
            ans.push(prefix_sum_set.contains(&target));

            prefix_sum_set.insert(prefix_dirs_sum[t]);
        }

        Answer { ans }
    }

    #[allow(dead_code)]
    fn solve_naive(&self) -> Answer {
        todo!();
        // let ans = 0;
        // Answer { ans }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Answer {
    ans: Vec<bool>,
}

impl Answer {
    fn print(&self) {
        let msg = self
            .ans
            .iter()
            .copied()
            .map(|p| if p { '1' } else { '0' })
            .collect_vec();
        print_chars(&msg);
    }
}

fn main() {
    Problem::read().solve().print();
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;
    #[allow(unused_imports)]
    use rand::{rngs::SmallRng, seq::SliceRandom, *};

    #[test]
    fn test_problem() {
        assert_eq!(1 + 1, 2);
    }

    #[allow(dead_code)]
    #[derive(Debug)]
    struct WrongTestCase {
        problem: Problem,
        main_ans: Answer,
        naive_ans: Answer,
    }

    #[allow(dead_code)]
    fn check(p: &Problem) -> Option<WrongTestCase> {
        let main_ans = p.solve();
        let naive_ans = p.solve_naive();
        if main_ans != naive_ans {
            Some(WrongTestCase {
                problem: p.clone(),
                main_ans,
                naive_ans,
            })
        } else {
            None
        }
    }

    #[allow(dead_code)]
    fn make_random_problem(rng: &mut SmallRng) -> Problem {
        todo!()
        // let n = rng.gen_range(1..=10);
        // let p = Problem { _a: n };
        // println!("{:?}", &p);
        // p
    }

    #[allow(unreachable_code)]
    #[test]
    fn test_with_naive() {
        let num_tests = 0;
        let max_wrong_case = 10; // この件数間違いが見つかったら打ち切り
        let mut rng = SmallRng::seed_from_u64(42);
        // let mut rng = SmallRng::from_entropy();
        let mut wrong_cases: Vec<WrongTestCase> = vec![];
        for _ in 0..num_tests {
            let p = make_random_problem(&mut rng);
            let result = check(&p);
            if let Some(wrong_test_case) = result {
                wrong_cases.push(wrong_test_case);
            }
            if wrong_cases.len() >= max_wrong_case {
                break;
            }
        }

        if !wrong_cases.is_empty() {
            for t in &wrong_cases {
                println!("{:?}", t.problem);
                println!("main ans : {:?}", t.main_ans);
                println!("naive ans: {:?}", t.naive_ans);
                println!();
            }
            println!("{} cases are wrong.", wrong_cases.len());
            panic!();
        }
    }
}

// ====== import ======
#[allow(unused_imports)]
use itertools::{chain, iproduct, izip, Itertools};
#[allow(unused_imports)]
use proconio::{
    derive_readable, fastout, input,
    marker::{Bytes, Chars, Usize1},
};
#[allow(unused_imports)]
use std::cmp::Reverse;
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap, HashSet};

// ====== output func ======
#[allow(unused_imports)]
use print_vec::*;
pub mod print_vec {

    use itertools::Itertools;
    use proconio::fastout;
    #[fastout]
    pub fn print_vec<T: std::fmt::Display>(arr: &[T]) {
        for a in arr {
            println!("{}", a);
        }
    }
    #[fastout]
    pub fn print_vec_1line<T: std::fmt::Display>(arr: &[T]) {
        let msg = arr.iter().map(|x| format!("{}", x)).join(" ");
        println!("{}", msg);
    }
    #[fastout]
    pub fn print_vec2<T: std::fmt::Display>(arr: &Vec<Vec<T>>) {
        for row in arr {
            let msg = row.iter().map(|x| format!("{}", x)).join(" ");
            println!("{}", msg);
        }
    }
    pub fn print_bytes(bytes: &[u8]) {
        let msg = String::from_utf8(bytes.to_vec()).unwrap();
        println!("{}", msg);
    }
    pub fn print_chars(chars: &[char]) {
        let msg = chars.iter().collect::<String>();
        println!("{}", msg);
    }
    #[fastout]
    pub fn print_vec_bytes(vec_bytes: &[Vec<u8>]) {
        for row in vec_bytes {
            let msg = String::from_utf8(row.to_vec()).unwrap();
            println!("{}", msg);
        }
    }
}

#[allow(unused)]
fn print_yesno(ans: bool) {
    let msg = if ans { "Yes" } else { "No" };
    println!("{}", msg);
}

// ====== snippet ======
use pos::*;
pub mod pos {
    use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};
    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Pos {
        pub x: i64,
        pub y: i64,
    }
    impl Pos {
        pub fn new(x: i64, y: i64) -> Pos {
            Pos { x, y }
        }
    }
    impl Pos {
        pub fn scala_mul(self, rhs: i64) -> Pos {
            Pos::new(self.x * rhs, self.y * rhs)
        }
    }
    impl Pos {
        pub fn inner_product(self, rhs: Self) -> i64 {
            self.x * rhs.x + self.y * rhs.y
        }
        pub fn norm_square(self) -> i64 {
            self.inner_product(self)
        }
    }
    impl Add for Pos {
        type Output = Pos;
        fn add(self, rhs: Self) -> Self::Output {
            Pos::new(self.x + rhs.x, self.y + rhs.y)
        }
    }
    impl Sub for Pos {
        type Output = Pos;
        fn sub(self, rhs: Self) -> Self::Output {
            Pos::new(self.x - rhs.x, self.y - rhs.y)
        }
    }
    impl Neg for Pos {
        type Output = Self;
        fn neg(self) -> Self::Output {
            Pos::new(-self.x, -self.y)
        }
    }
    impl num_traits::Zero for Pos {
        fn zero() -> Self {
            Pos::new(0, 0)
        }
        fn is_zero(&self) -> bool {
            self.x.is_zero() && self.y.is_zero()
        }
    }
    impl AddAssign for Pos {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs
        }
    }
    impl SubAssign for Pos {
        fn sub_assign(&mut self, rhs: Self) {
            *self = *self - rhs
        }
    }
    pub const DIR8_LIST: [Pos; 8] = [
        Pos { x: 0, y: 1 },
        Pos { x: 1, y: 1 },
        Pos { x: 1, y: 0 },
        Pos { x: 1, y: -1 },
        Pos { x: 0, y: -1 },
        Pos { x: -1, y: -1 },
        Pos { x: -1, y: 0 },
        Pos { x: -1, y: 1 },
    ];
    pub const DIR4_LIST: [Pos; 4] = [
        Pos { x: 0, y: 1 },
        Pos { x: 1, y: 0 },
        Pos { x: 0, y: -1 },
        Pos { x: -1, y: 0 },
    ];
    impl Pos {
        pub fn around4_pos_iter(self) -> impl Iterator<Item = Pos> {
            DIR4_LIST.iter().copied().map(move |d| self + d)
        }
        pub fn around8_pos_iter(self) -> impl Iterator<Item = Pos> {
            DIR8_LIST.iter().copied().map(move |d| self + d)
        }
    }
}

Submission Info

Submission Time
Task D - Bonfire
User paruma184
Language Rust (rustc 1.70.0)
Score 425
Code Size 8135 Byte
Status AC
Exec Time 25 ms
Memory 15788 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 63
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt, test_60.txt
Case Name Status Exec Time Memory
sample_01.txt AC 0 ms 2016 KB
sample_02.txt AC 1 ms 1984 KB
sample_03.txt AC 0 ms 1964 KB
test_01.txt AC 17 ms 10888 KB
test_02.txt AC 18 ms 10988 KB
test_03.txt AC 18 ms 10812 KB
test_04.txt AC 1 ms 2080 KB
test_05.txt AC 17 ms 10896 KB
test_06.txt AC 18 ms 10964 KB
test_07.txt AC 18 ms 10984 KB
test_08.txt AC 0 ms 1960 KB
test_09.txt AC 18 ms 10828 KB
test_10.txt AC 17 ms 10824 KB
test_11.txt AC 17 ms 10956 KB
test_12.txt AC 0 ms 1884 KB
test_13.txt AC 17 ms 10812 KB
test_14.txt AC 17 ms 10984 KB
test_15.txt AC 17 ms 10984 KB
test_16.txt AC 1 ms 2016 KB
test_17.txt AC 17 ms 10972 KB
test_18.txt AC 17 ms 10900 KB
test_19.txt AC 17 ms 10900 KB
test_20.txt AC 4 ms 4004 KB
test_21.txt AC 17 ms 10820 KB
test_22.txt AC 17 ms 10960 KB
test_23.txt AC 17 ms 10892 KB
test_24.txt AC 17 ms 10916 KB
test_25.txt AC 23 ms 15716 KB
test_26.txt AC 24 ms 15716 KB
test_27.txt AC 24 ms 15656 KB
test_28.txt AC 24 ms 15676 KB
test_29.txt AC 24 ms 15692 KB
test_30.txt AC 24 ms 15784 KB
test_31.txt AC 23 ms 15752 KB
test_32.txt AC 23 ms 15680 KB
test_33.txt AC 23 ms 15776 KB
test_34.txt AC 24 ms 15680 KB
test_35.txt AC 24 ms 15784 KB
test_36.txt AC 24 ms 15560 KB
test_37.txt AC 23 ms 15784 KB
test_38.txt AC 24 ms 15788 KB
test_39.txt AC 24 ms 15676 KB
test_40.txt AC 24 ms 15696 KB
test_41.txt AC 20 ms 12568 KB
test_42.txt AC 24 ms 15632 KB
test_43.txt AC 24 ms 15636 KB
test_44.txt AC 24 ms 15644 KB
test_45.txt AC 20 ms 12520 KB
test_46.txt AC 20 ms 12364 KB
test_47.txt AC 24 ms 15636 KB
test_48.txt AC 21 ms 12572 KB
test_49.txt AC 23 ms 15716 KB
test_50.txt AC 24 ms 15684 KB
test_51.txt AC 24 ms 15776 KB
test_52.txt AC 24 ms 15664 KB
test_53.txt AC 21 ms 12588 KB
test_54.txt AC 25 ms 15712 KB
test_55.txt AC 25 ms 15788 KB
test_56.txt AC 23 ms 15692 KB
test_57.txt AC 24 ms 15644 KB
test_58.txt AC 23 ms 15720 KB
test_59.txt AC 20 ms 12520 KB
test_60.txt AC 24 ms 15660 KB


2025-04-11 (Fri)
19:26:27 +00:00