結果

問題 No.861 ケーキカット
ユーザー chocoruskchocorusk
提出日時 2019-08-10 00:35:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 505 ms / 1,000 ms
コード長 1,475 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 98,092 KB
実行使用メモリ 19,060 KB
最終ジャッジ日時 2023-09-22 02:17:53
合計ジャッジ時間 14,149 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 489 ms
18,972 KB
testcase_01 AC 492 ms
18,796 KB
testcase_02 AC 494 ms
18,784 KB
testcase_03 AC 499 ms
18,908 KB
testcase_04 AC 494 ms
18,916 KB
testcase_05 AC 493 ms
18,784 KB
testcase_06 AC 484 ms
18,804 KB
testcase_07 AC 487 ms
18,792 KB
testcase_08 AC 494 ms
18,788 KB
testcase_09 AC 490 ms
18,776 KB
testcase_10 AC 486 ms
18,796 KB
testcase_11 AC 483 ms
18,876 KB
testcase_12 AC 486 ms
18,912 KB
testcase_13 AC 485 ms
18,780 KB
testcase_14 AC 488 ms
19,060 KB
testcase_15 AC 484 ms
18,888 KB
testcase_16 AC 493 ms
18,812 KB
testcase_17 AC 500 ms
18,780 KB
testcase_18 AC 502 ms
18,812 KB
testcase_19 AC 496 ms
18,824 KB
testcase_20 AC 503 ms
18,780 KB
testcase_21 AC 499 ms
18,804 KB
testcase_22 AC 504 ms
18,812 KB
testcase_23 AC 505 ms
18,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll c[25];
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
bool used[1<<25];
void dfs(int b){
    used[b]=1;
    for(int i=0; i<25; i++){
        if((b&(1<<i))==0){
            if(used[b^(1<<i)]) continue;
            int x=i/5, y=i%5;
            for(int k=0; k<4; k++){
                int x1=x+dx[k], y1=y+dy[k], t=x1*5+y1;
                if(x1<0 || x1>=5 || y1<0 || y1>=5) continue;
                if(b&(1<<t)){
                    dfs(b^(1<<i));
                    break;
                }
            }
        }
    }
}
int main()
{
    for(int i=0; i<5; i++) for(int j=0; j<5; j++) cin>>c[i*5+j];
    for(int i=0; i<25; i++) dfs(1<<i);
    ll ans=1e18;
    for(int i=0; i<(1<<25); i++){
        if(used[i] && used[(1<<25)-1-i]){
            ll cnt0=0, cnt1=0;
            for(int j=0; j<25; j++){
                if(i&(1<<j)) cnt1+=c[j];
                else cnt0+=c[j];
            }
            ans=min(ans, abs(cnt0-cnt1));
        }
    }
    cout<<ans<<endl;
    return 0;
}
0