Submission #62307518


Source Code Expand

#[allow(unused_imports)]
use itertools::Itertools;
use proconio::input;
use proconio::marker::Chars;

struct Node {
    color: char,
    cost: i32,
}

impl Node {
    fn new(color: char, cost: i32) -> Self {
        Self { color, cost }
    }

    fn vote(v: &Vec<Node>) -> (char, i32) {
        let col = if v.iter().filter(|c| c.color == '0').count() >= 2 {
            '0'
        } else {
            '1'
        };
        let other = if col == '0' {
            '1'
        } else {
            '0'
        };
        let c = col;
        let cost = v.iter()
            .map(|c| if c.color == other { 0 } else { c.cost })
            .sorted_unstable()
            .enumerate()
            .map(|(i, c)| if i == 2 { 0 } else { c })
            .sum::<i32>();
        (c, cost)
    }
}

fn dfs(i: usize, n: usize, offset: usize, s: &Vec<char>) -> Node {
    if i == n {
        return Node::new(s[offset], 1);
    }

    let k = s.len() / 3usize.pow(i as u32+1);
    let mut v = vec![];
    for j in 0..3 {
        v.push(dfs(i+1, n, offset + k*j, s));
    }
    let (c, cost) = Node::vote(&v);
    Node::new(c, cost)
}

fn main() {
    input!{
        n: usize,
        s: Chars,
    }
    let res = dfs(0, n, 0, &s);
    println!("{}", res.cost);
}

pub trait Debuggable {
    fn debug_string(&self) -> String;
}

impl<T: std::fmt::Debug + std::fmt::Display> Debuggable for Vec<T> {
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        use std::iter::repeat;
        if let Some(max_size) = self.iter()
            .enumerate()
            .map(|(i, x)| (format!("{:?}", x).len()).max(format!("{:?}", i).len()))
            .max() {
                let mut idx = String::from("idx |");   
                let mut val = String::from("val |");   
                for (i, xi) in self.iter().enumerate() {
                    idx.push_str(
                        &format!(" {:>w$} |", i, w=max_size)
                    );
                    val.push_str(
                        &format!(" {:>w$} |", xi, w=max_size)
                    );
                }

                format!("{}\n{}\n{}\n", idx, repeat('-').take(idx.len()).join(""), val)
            } else {
                format!("idx | \nval |\n")
            }
    }
}

impl<T: std::fmt::Debug + std::fmt::Display> Debuggable for std::collections::BTreeSet<T> {
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        format!("{{ {} }}", self.iter().join(", "))
    }
}

impl<T, U> Debuggable for std::collections::BTreeMap<T, U> 
where T: std::fmt::Debug + std::fmt::Display, U: std::fmt::Debug + std::fmt::Display
{
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        format!(
            "{{\n{}\n }}", self.iter()
                .map(|(k, v)| format!("{k} -> {v}, "))
                .join("\n")
        )
    }
}

// lg! マクロの定義
#[macro_export]
macro_rules! lg {
    ($val:expr) => {
        #[cfg(feature = "local")]
        {
            {
                use Debuggable;
                let val = &$val;
                eprintln!(
                    "[{}:{}] {} = \n{}",
                    file!(),
                    line!(),
                    stringify!($val),
                    val.debug_string()
                );
                val
            }
        }
    }
}
    

Submission Info

Submission Time
Task E - Hierarchical Majority Vote
User ardRiriy
Language Rust (rustc 1.70.0)
Score 450
Code Size 3355 Byte
Status AC
Exec Time 53 ms
Memory 9640 KiB

Compile Error

warning: the item `Itertools` is imported redundantly
  --> src/main.rs:67:13
   |
2  | use itertools::Itertools;
   |     -------------------- the item `Itertools` is already imported here
...
67 |         use itertools::Itertools;
   |             ^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: the item `Itertools` is imported redundantly
  --> src/main.rs:93:13
   |
2  | use itertools::Itertools;
   |     -------------------- the item `Itertools` is already imported here
...
93 |         use itertools::Itertools;
   |             ^^^^^^^^^^^^^^^^^^^^

warning: the item `Itertools` is imported redundantly
   --> src/main.rs:102:13
    |
2   | use itertools::Itertools;
    |     -------------------- the item `Itertools` is already imported here
...
102 |         use itertools::Itertools;
    |             ^^^^^^^^^^^^^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 32
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt
All 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 1 ms 1808 KiB
00_sample_02.txt AC 1 ms 1940 KiB
01_random_01.txt AC 1 ms 2168 KiB
01_random_02.txt AC 52 ms 9544 KiB
01_random_03.txt AC 1 ms 1944 KiB
01_random_04.txt AC 52 ms 9532 KiB
01_random_05.txt AC 3 ms 2168 KiB
01_random_06.txt AC 49 ms 9432 KiB
01_random_07.txt AC 7 ms 2752 KiB
01_random_08.txt AC 52 ms 9480 KiB
01_random_09.txt AC 1 ms 1932 KiB
01_random_10.txt AC 51 ms 9600 KiB
01_random_11.txt AC 1 ms 1952 KiB
01_random_12.txt AC 43 ms 9476 KiB
01_random_13.txt AC 1 ms 1880 KiB
01_random_14.txt AC 53 ms 9424 KiB
01_random_15.txt AC 1 ms 2120 KiB
01_random_16.txt AC 50 ms 9624 KiB
01_random_17.txt AC 1 ms 2168 KiB
01_random_18.txt AC 51 ms 9516 KiB
01_random_19.txt AC 1 ms 1988 KiB
01_random_20.txt AC 52 ms 9520 KiB
02_handmade_01.txt AC 43 ms 9548 KiB
02_handmade_02.txt AC 41 ms 9596 KiB
02_handmade_03.txt AC 43 ms 9536 KiB
02_handmade_04.txt AC 42 ms 9572 KiB
02_handmade_05.txt AC 43 ms 9640 KiB
02_handmade_06.txt AC 42 ms 9584 KiB
02_handmade_07.txt AC 42 ms 9596 KiB
02_handmade_08.txt AC 43 ms 9568 KiB
02_handmade_09.txt AC 41 ms 9492 KiB
02_handmade_10.txt AC 41 ms 9524 KiB