結果

問題 No.861 ケーキカット
ユーザー mugen_1337mugen_1337
提出日時 2021-02-20 15:39:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,897 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 206,292 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 22:52:04
合計ジャッジ時間 22,527 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;}

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;
}
0