結果

問題 No.733 分身並列コーディング
ユーザー koba-e964koba-e964
提出日時 2021-11-10 22:03:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 181 ms / 1,500 ms
コード長 2,300 bytes
コンパイル時間 12,216 ms
コンパイル使用メモリ 404,096 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 10:38:18
合計ジャッジ時間 17,009 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 87 ms
5,248 KB
testcase_04 AC 87 ms
5,376 KB
testcase_05 AC 177 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 174 ms
5,376 KB
testcase_08 AC 178 ms
5,376 KB
testcase_09 AC 35 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 34 ms
5,376 KB
testcase_20 AC 40 ms
5,376 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 93 ms
5,376 KB
testcase_23 AC 13 ms
5,376 KB
testcase_24 AC 16 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 181 ms
5,376 KB
testcase_29 AC 137 ms
5,376 KB
testcase_30 AC 131 ms
5,376 KB
testcase_31 AC 131 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 8 ms
5,376 KB
testcase_38 AC 155 ms
5,376 KB
testcase_39 AC 168 ms
5,376 KB
testcase_40 AC 140 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 7 ms
5,376 KB
testcase_43 AC 8 ms
5,376 KB
testcase_44 AC 152 ms
5,376 KB
testcase_45 AC 169 ms
5,376 KB
testcase_46 AC 172 ms
5,376 KB
testcase_47 AC 159 ms
5,376 KB
testcase_48 AC 160 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/// Generates an Iterator over subsets of univ, in the descending order. 
/// Verified by: http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3050308
struct SubsetIter { bits: Option<usize>, univ: usize }
impl Iterator for SubsetIter {
    type Item = usize;
    fn next(&mut self) -> Option<usize> {
        match self.bits {
            None => None,
            Some(bits) => {
                let ans = bits;
                self.bits =
                    if bits == 0 { None }
                else { Some((bits - 1) & self.univ) };
                Some(ans)
            }
        }
    }
}
fn subsets(univ: usize) -> SubsetIter {
    SubsetIter { bits: Some(univ), univ: univ }
}

fn main() {
    input! {
        t: i64,
        n: usize,
        a: [i64; n],
    }
    let mut ok = vec![false; 1 << n];
    for bits in 1..1 << n {
        let mut s = 0;
        for i in 0..n {
            if (bits & 1 << i) != 0 { s += a[i]; }
        }
        ok[bits] = s <= t;
    }
    let mut dp = vec![0; 1 << n];
    for bits in 1usize..1 << n {
        let mut me = 100;
        let lsb = bits & bits.wrapping_neg();
        for sub in subsets(bits ^ lsb) {
            if ok[sub | lsb] {
                me = min(me, dp[bits ^ sub ^ lsb] + 1);
            }
        }
        dp[bits] = me;
    }
    println!("{}", dp[(1 << n) - 1]);
}
0