結果

問題 No.861 ケーキカット
ユーザー koba-e964koba-e964
提出日時 2022-01-01 17:07:46
言語 Rust
(1.77.0)
結果
AC  
実行時間 437 ms / 1,000 ms
コード長 2,005 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 173,836 KB
実行使用メモリ 50,224 KB
最終ジャッジ日時 2024-04-18 14:57:44
合計ジャッジ時間 11,621 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 388 ms
50,224 KB
testcase_01 AC 378 ms
50,100 KB
testcase_02 AC 380 ms
50,100 KB
testcase_03 AC 399 ms
50,100 KB
testcase_04 AC 419 ms
50,048 KB
testcase_05 AC 388 ms
50,096 KB
testcase_06 AC 385 ms
50,176 KB
testcase_07 AC 411 ms
50,096 KB
testcase_08 AC 398 ms
50,096 KB
testcase_09 AC 422 ms
50,092 KB
testcase_10 AC 401 ms
50,176 KB
testcase_11 AC 403 ms
50,096 KB
testcase_12 AC 400 ms
50,092 KB
testcase_13 AC 407 ms
50,176 KB
testcase_14 AC 386 ms
50,092 KB
testcase_15 AC 408 ms
50,220 KB
testcase_16 AC 387 ms
50,092 KB
testcase_17 AC 395 ms
50,096 KB
testcase_18 AC 402 ms
50,096 KB
testcase_19 AC 391 ms
50,100 KB
testcase_20 AC 391 ms
50,096 KB
testcase_21 AC 396 ms
50,096 KB
testcase_22 AC 414 ms
50,096 KB
testcase_23 AC 437 ms
50,100 KB
権限があれば一括ダウンロードができます

ソースコード

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

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;
        let mut adj = (v << 1) & 0x1ef7bde;
        adj |= (v >> 1) & 0xf7bdef;
        adj |= v << 5;
        adj |= v >> 5;
        adj &= !v;
        for i in 0..25 {
            if (adj & 1 << i) == 0 { continue; }
            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