結果

問題 No.861 ケーキカット
ユーザー koba-e964koba-e964
提出日時 2022-01-01 17:02:03
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,858 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 171,216 KB
実行使用メモリ 161,536 KB
最終ジャッジ日時 2024-04-18 14:52:48
合計ジャッジ時間 6,225 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated items `new` and `unite` are never used
  --> main.rs:41:8
   |
40 | impl UnionFind {
   | -------------- associated items in this implementation
41 |     fn new(n: usize) -> Self {
   |        ^^^
...
53 |     fn unite(&mut self, x: usize, y: usize) {
   |        ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: function `is_conn` is never used
  --> main.rs:74:4
   |
74 | fn is_conn(bits: i32) -> bool {
   |    ^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

use std::cmp::*;
use std::collections::*;
// 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;
    let mut is_conn = vec![false; 1 << 25];
    let mut que = VecDeque::new();
    for i in 0..25 {
        que.push_back(1 << i);
    }
    while let Some(v) = que.pop_front() {
        if is_conn[v] { continue; }
        is_conn[v] = true;
        for i in 0..25 {
            if (v & 1 << i) != 0 { continue; }
            let mut mask = (5 << (i % 5)) >> 1 << (i / 5 * 5);
            if i >= 5 {
                mask |= 1 << (i - 5);
            }
            mask |= 1 << (i + 5);
            if (v & mask) != 0 {
                let new = v | 1 << i;
                if is_conn[new] { continue; }
                que.push_back(new);
            }
        }
    }
    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 -= 2 * c[i];
                }
            }
            mi = min(mi, tmp.abs());
        }
    }
    println!("{}", mi);
}
0