結果

問題 No.861 ケーキカット
ユーザー MisterMister
提出日時 2020-08-05 12:14:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,716 bytes
コンパイル時間 854 ms
コンパイル使用メモリ 78,444 KB
実行使用メモリ 12,016 KB
最終ジャッジ日時 2023-10-14 10:25:01
合計ジャッジ時間 6,009 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 #

#include <iostream>
#include <numeric>
#include <vector>

struct UnionFind {
    std::vector<int> par;
    int gnum;

    explicit UnionFind(int n)
        : par(n), gnum(n) {
        std::iota(par.begin(), par.end(), 0);
    }

    void reset() {
        std::iota(par.begin(), par.end(), 0);
        gnum = par.size();
    }

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        par[v] = u;
        --gnum;
    }
};

using lint = long long;

constexpr lint INF = 1LL << 60;
constexpr int N = 5;
constexpr int M = N * N;

const std::vector<int> ds{1, N};

UnionFind uf(M);

bool connected(int b) {
    uf.reset();

    for (int i = 0; i < M; ++i) {
        {
            int j = i + 1;
            if (j < M && ((b >> i) & 1) == ((b >> j) & 1)) {
                uf.unite(i, j);
            }
        }
        {
            int j = i + N;
            if (j < M && ((b >> i) & 1) == ((b >> j) & 1)) {
                uf.unite(i, j);
            }
        }
    }

    return uf.gnum == 2;
}

void solve() {
    std::vector<lint> xs(M);
    for (auto& x : xs) std::cin >> x;

    lint ans = INF;
    for (int b = 0; b < (1 << (M - 1)); ++b) {
        if (!connected(b)) continue;

        lint score = 0;
        for (int i = 0; i < M; ++i) {
            if ((b >> i) & 1) {
                score += xs[i];
            } else {
                score -= xs[i];
            }
        }
        ans = std::min(ans, std::abs(score));
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0