提出 #65479955


ソースコード 拡げる

#![allow(unused)]

fn main() {
    let inp = readv::<usize>();
    let (n, m) = (inp[0], inp[1]);
    let fee = readv::<i64>();
    let mut animals = vec![vec![]; n];
    for animal_id in 0..m {
        let inp = readv::<usize>();
        for zoo in &inp[1..] {
            let zoo = zoo - 1;
            animals[zoo].push(animal_id);
        }
    }

    let mut ans = i64::MAX;
    for mask in 0..(1 << n) {
        for submask in submask_iter(mask) {
            let mut cnt = vec![0; m];
            let mut sum = 0;

            for zoo in 0..n {
                let mut times = 0;
                times += (mask >> zoo) & 1;
                times += (submask >> zoo) & 1;
                if times > 0 {
                    sum += times as i64 * fee[zoo];
                    for &animal in &animals[zoo] {
                        cnt[animal] += times;
                    }
                }
            }

            if (0..m).all(|i| cnt[i] >= 2) {
                ans = ans.min(sum);
            }
        }
    }

    println!("{}", ans);
}

fn submask_iter(mask: usize) -> impl std::iter::Iterator<Item = usize> {
    let mut submask = mask;
    let iter1 = std::iter::once(submask);
    let iter2 = std::iter::from_fn(move || {
        if submask == 0 {
            None
        } else {
            submask = (submask - 1) & mask;
            Some(submask)
        }
    });
    iter1.chain(iter2)
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s);
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 D - Goin' to the Zoo
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 400
コード長 2132 Byte
結果 AC
実行時間 29 ms
メモリ 2088 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 2
AC × 35
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All hand_01.txt, hand_02.txt, hand_03.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 1 ms 1896 KiB
hand_02.txt AC 1 ms 1932 KiB
hand_03.txt AC 1 ms 1936 KiB
random_01.txt AC 29 ms 1952 KiB
random_02.txt AC 29 ms 1952 KiB
random_03.txt AC 14 ms 2088 KiB
random_04.txt AC 17 ms 1936 KiB
random_05.txt AC 3 ms 1928 KiB
random_06.txt AC 2 ms 1916 KiB
random_07.txt AC 19 ms 1824 KiB
random_08.txt AC 1 ms 1808 KiB
random_09.txt AC 3 ms 2060 KiB
random_10.txt AC 1 ms 1964 KiB
random_11.txt AC 17 ms 1876 KiB
random_12.txt AC 1 ms 1920 KiB
random_13.txt AC 4 ms 1916 KiB
random_14.txt AC 1 ms 1992 KiB
random_15.txt AC 16 ms 1888 KiB
random_16.txt AC 1 ms 2076 KiB
random_17.txt AC 4 ms 1796 KiB
random_18.txt AC 1 ms 2076 KiB
random_19.txt AC 3 ms 1960 KiB
random_20.txt AC 5 ms 1928 KiB
random_21.txt AC 1 ms 2000 KiB
random_22.txt AC 3 ms 1920 KiB
random_23.txt AC 1 ms 2084 KiB
random_24.txt AC 2 ms 1800 KiB
random_25.txt AC 1 ms 1928 KiB
random_26.txt AC 4 ms 1864 KiB
random_27.txt AC 1 ms 2060 KiB
random_28.txt AC 1 ms 2016 KiB
random_29.txt AC 1 ms 1924 KiB
random_30.txt AC 2 ms 1972 KiB
sample_01.txt AC 1 ms 1996 KiB
sample_02.txt AC 1 ms 2060 KiB