結果

問題 No.861 ケーキカット
ユーザー koba-e964koba-e964
提出日時 2021-12-31 16:01:04
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,159 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 175,356 KB
実行使用メモリ 15,544 KB
最終ジャッジ日時 2024-04-17 03:53:48
合計ジャッジ時間 5,353 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

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"));
}

/**
 * Union-Find tree.
 * Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305
 */
struct UnionFind { disj: Vec<usize>, rank: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let disj = (0..n).collect();
        UnionFind { disj: disj, rank: vec![1; n] }
    }
    fn root(&mut self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        self.disj[x]
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.rank[x] > self.rank[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.disj[x] = y;
        self.rank[y] += self.rank[x];
    }
    #[allow(unused)]
    fn is_same_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&mut self, x: usize) -> usize {
        let x = self.root(x);
        self.rank[x]
    }
}

fn is_conn(bits: i32) -> bool {
    let mut uf = UnionFind::new(25);
    for i in 0..5 {
        for j in 0..4 {
            let v = i * 5 + j;
            if (bits & 3 << v) == (3 << v) {
                uf.unite(v, v + 1);
            }
        }
    }
    for i in 0..4 {
        for j in 0..5 {
            let v = i * 5 + j;
            if (bits & 33 << v) == (33 << v) {
                uf.unite(v, v + 5);
            }
        }
    }
    let mut tmp = 25;
    for i in 0..25 {
        if (bits & 1 << i) == 0 { continue; }
        let r = uf.root(i);
        if tmp == 25 {
            tmp = r;
        } else if tmp != r {
            return false;
        }
    }
    true
}

fn main() {
    input!(c: [i64; 25]);
    let s: i64 = c.iter().sum();
    let mut mi = 1i64 << 50;
    for bits in 1..1 << 24 {
        if is_conn(bits) && is_conn((1 << 25) - 1 - bits) {
            let mut tmp = s;
            for i in 0..25 {
                if (bits & 1 << i) != 0 {
                    tmp -= c[i];
                }
            }
            mi = min(mi, tmp.abs());
        }
    }
    println!("{}", mi);
}
0