結果
| 問題 | No.861 ケーキカット | 
| コンテスト | |
| ユーザー |  mugen_1337 | 
| 提出日時 | 2021-02-20 15:40:59 | 
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 2,913 bytes | 
| コンパイル時間 | 7,051 ms | 
| コンパイル使用メモリ | 258,512 KB | 
| 最終ジャッジ日時 | 2025-01-19 02:42:24 | 
| ジャッジサーバーID (参考情報) | judge1 / judge6 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 20 TLE * 1 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
#define int ll
struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}
struct UnionFind{
    private:
    vector<int> par,siz;
    public:
    int con;
    UnionFind(int n):par(n),siz(n,1),con(n){
        iota(begin(par),end(par),0);
    }
    int root(int x){
        return (par[x]==x?x:(par[x]=root(par[x])));
    }
    bool sameroot(int x,int y){
        return root(x)==root(y);
    }
    bool unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y) return false;
        if(siz[x]<siz[y])swap(x,y);
        siz[x]+=siz[y];
        par[y]=x;
        con--;
        return true;
    }
    int size(int x){
        return siz[root(x)];
    }
};
bool check(int i){
    int g[5][5]={};
    rep(j,25){
        int pi=j/5,pj=j%5;
        g[pi][pj]=(i>>j)&1;
    }
    UnionFind uf(25);
    rep(pi,5)rep(pj,5){
        rep(k,4){
            int ni=pi+dx[k],nj=pj+dy[k];
            if(0<=ni and ni<5 and 0<=nj and nj<5){
                if(g[ni][nj]==g[pi][pj]) uf.unite(ni*5+nj,pi*5+pj);
            }
        }
    }
    return uf.con==2;
}
vector<int> v(25);
int q(int i){
    int s=0;
    rep(j,25){
        if((i>>j)&1) s+=v[j];
        else         s-=v[j];
    }
    return abs(s);
}
signed main(){
    cin>>v;
    const int x[]={0,1,2,3,4,9,14,19,24,23,22,21,20,15,10,5};
    const int y[]={6,7,8,11,12,13,16,17,18};//  
    int ans=INF;
    {
        rep(i,1<<9){
            int bit=0;
            rep(j,9)if((i>>j)&1) bit+=(1<<y[j]);
            if(check(bit)) chmin(ans,q(bit));
        }
    }
    {
        for(int w=1;w<16;w++){
            for(int st=0;st<16;st++){
                int base=0;
                for(int p=st;p<st+w;p++){
                    base+=(1<<x[p%16]);
                    rep(i,1<<9){
                        int bit=base;
                        rep(j,9)if((i>>j)&1) bit+=(1<<y[j]);
                        if(check(bit)) chmin(ans,q(bit));
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
            
            
            
        