結果

問題 No.861 ケーキカット
ユーザー tempura_pptempura_pp
提出日時 2019-08-09 23:03:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,813 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 99,708 KB
実行使用メモリ 11,792 KB
最終ジャッジ日時 2023-09-26 21:42:22
合計ジャッジ時間 5,546 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;


struct UnionFind{
   vector<int> par;
   UnionFind(int n):par(n,-1){}
   int find(int x){
       if(par[x]<0)return x;
       return par[x]=find(par[x]);
   }
   bool unite(int x,int y){
       x=find(x);
       y=find(y);
       if(x==y)return false;
       if(par[x]>par[y]){
           par[y]+=par[x];
           par[x]=y;
       }
       else{
           par[x]+=par[y];
           par[y]=x;
       }
       return true;
   }

   bool same(int x,int y){
       return find(x)==find(y);
   }
   int size(int x){
       return -par[find(x)];
   }
};
int main(){
    ll a[5][5];
    rep(i,5)rep(j,5)cin>>a[i][j];
    ll ans = longinf;
    int b[5][5];
    rep(mask,1<<24){
        if(mask==0)continue;
        ll sum = a[4][4];
        b[4][4]=0;
        rep(j,5)rep(k,5){
            if(j==4&&k==4)break;
            b[j][k]=(mask>>(5*j+k))&1;
            if(b[j][k])sum-=a[j][k];
            else sum+=a[j][k];
        }
        UnionFind uf(25);
        rep(i,5)rep(j,4){
            if(b[i][j]==b[i][j+1])uf.unite(5*i+j,5*i+j+1);
        }
        rep(i,4)rep(j,5){
            if(b[i][j]==b[i+1][j])uf.unite(5*i+j,5*i+j+5);
        }
        int idx = 0;
        while(b[idx/5][idx%5]==0)++idx;
        if(uf.size(idx)+uf.size(24)==25){
            ans = min(ans,abs(sum));
        }
    }
    cout<<ans<<endl;
    return 0;
}
0