結果

問題 No.861 ケーキカット
ユーザー otoshigootoshigo
提出日時 2024-01-27 16:27:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 1,000 ms
コード長 3,017 bytes
コンパイル時間 1,222 ms
コンパイル使用メモリ 96,472 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-01-27 16:27:11
合計ジャッジ時間 4,595 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

struct UnionFind {
private:
    vector<int> par, siz;

public:
    UnionFind(int n) : par(n, -1), siz(n, 1) {}
    int find(int x) {
        if (par[x] == -1) return x;
        else {
            par[x] = find(par[x]);
            return par[x];
        }
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        return siz[find(x)];
    }
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }
};

const int dx[]={1,0},dy[]={0,1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    N=5;
    vector C(N,vector<ll>(N));
    for(int i=0;i<N;i++)for(int j=0;j<N;j++)cin>>C[i][j];
    vector<pair<int,int>>V;
    for(int i=0;i<N-1;i++)V.push_back(make_pair(0,i));
    for(int i=0;i<N-1;i++)V.push_back(make_pair(i,N-1));
    for(int i=0;i<N-1;i++)V.push_back(make_pair(N-1,N-1-i));
    for(int i=0;i<N-1;i++)V.push_back(make_pair(N-1-i,0));
    int L=V.size();
    ll ans=1LL<<60;
    for(int l=0;l<L;l++)for(int r=l;r<L;r++){
        for(int bit=0;bit<1<<((N-2)*(N-2));bit++){
            vector flag(N,vector<bool>(N));
            for(int i=l;i<=r;i++)flag[V[i].first][V[i].second]=true;
            for(int i=0;i<N-2;i++)for(int j=0;j<N-2;j++){
                if(bit>>((N-2)*i+j)&1){
                    flag[i+1][j+1]=true;
                }
            }
            UnionFind uf1(N*N),uf2(N*N);
            for(int i=0;i<N;i++)for(int j=0;j<N;j++){
                for(int k=0;k<2;k++){
                    int x=i+dx[k],y=j+dy[k];
                    if(x<0||x>=N||y<0||y>=N)continue;
                    if(flag[i][j]){
                        if(flag[x][y]){
                            uf1.merge(N*i+j,N*x+y);
                        }
                    }else{
                        if(!flag[x][y]){
                            uf2.merge(N*i+j,N*x+y);
                        }
                    }
                }
            }
            int c1=0,c2=0,s1=0,s2=0;
            ll a1=0,a2=0;
            for(int i=0;i<N;i++)for(int j=0;j<N;j++){
                if(flag[i][j]){
                    c1++;
                    a1+=C[i][j];
                }else{
                    c2++;
                    a2+=C[i][j];
                }
                if(s1==0&&flag[i][j])s1=uf1.size(N*i+j);
                if(s2==0&&!flag[i][j])s2=uf2.size(N*i+j);
            }
            if(c1==s1&&c2==s2){
                chmin(ans,abs(a1-a2));
            }
        }
    }
    cout<<ans<<"\n";
}
0