結果

問題 No.861 ケーキカット
ユーザー gratan_mogmoggratan_mogmog
提出日時 2019-08-10 06:37:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,355 bytes
コンパイル時間 1,903 ms
コンパイル使用メモリ 184,792 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 23:10:38
合計ジャッジ時間 6,662 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <iomanip>
#include <math.h>
#include <time.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define repf(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define repr(i, a, b) for (ll i = (ll)a; i > (ll)b; i--)
#define all(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int infi = 1147483600;
const ll infl = 1e17;
const char ENDL = '\n';

struct UnionFind
{
    vector<int> root;
    int n;
    void __init__(int sz)
    {
        n = sz;
        root.resize(n, -1);
    }
    int find(int x)
    {
        if (root[x] >= 0)
        {
            root[x] = find(root[x]);
            return root[x];
        }
        return x;
    }
    void unit(int a, int b)
    {
        int ra, rb;
        ra = find(a);
        rb = find(b);
        if (ra == rb)
        {
            return;
        }
        else if (root[ra] <= root[rb])
        {
            root[ra] += root[rb];
            root[rb] = ra;
        }
        else
        {
            root[rb] += root[ra];
            root[ra] = rb;
        }
        return;
    }
    int count()
    {
        set<int> cnt;
        rep(i, n)
            cnt.insert(find(i));
        return cnt.size();
    }
};

int main()
{
    vll MAP(25);
    rep(i, 25) cin >> MAP[i];
    vii move = {5, -5, 1, -1};
    map<int, int> mawari;
    rep(i, 5) mawari[i] = i;
    repf(i, 1, 5) mawari[i + 4] = 5 * i + 4;
    repr(i, 1, 5) mawari[i + 8] = 24-i;
    mawari[13] = 15;
    mawari[14] = 10;
    mawari[15] = 5;
    map<int, int> naka;
    repf(i,1,4){
        repf(j,1,4){
            naka[3 * (i - 1) + j - 1] = 5 * i + j;
        }
    }
    ll ans = infl;
    rep(i,16){
        repf(j,i,i+17){
            vii get(25, 0);
            rep(k,16){
                if (i<=k<j || k<j-16){
                    get[mawari[k]] = 1;
                }
            }
            rep(s,1ll<<9){
                rep(k,9){
                    if ((s>>k)&1){
                        get[naka[k]] = 1;
                    }
                    else{
                        get[naka[k]] = 0;
                    }
                }
                UnionFind uf;
                uf.__init__(25);
                ll now = 0;
                rep(ii,25){
                    if (get[ii])
                        now += MAP[ii];
                    else
                        now -= MAP[ii];
                    rep(m,4){
                        if (move[m]+ii>=0 && move[m]+ii<25){
                            if (get[move[m]+ii]==get[ii]){
                                uf.unit(move[m] + ii, ii);
                            }
                        }
                    }
                }
                if (uf.count()==2){
                    ans = min(ans, abs(now));
                }
            }
        }
    }
    cout << ans << ENDL;
}
0