結果

問題 No.861 ケーキカット
ユーザー lumc_lumc_
提出日時 2019-08-09 23:23:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 1,000 ms
コード長 5,388 bytes
コンパイル時間 1,076 ms
コンパイル使用メモリ 116,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 02:14:39
合計ジャッジ時間 5,501 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
4,384 KB
testcase_01 AC 121 ms
4,376 KB
testcase_02 AC 112 ms
4,380 KB
testcase_03 AC 113 ms
4,384 KB
testcase_04 AC 113 ms
4,380 KB
testcase_05 AC 321 ms
4,380 KB
testcase_06 AC 112 ms
4,380 KB
testcase_07 AC 113 ms
4,384 KB
testcase_08 AC 112 ms
4,380 KB
testcase_09 AC 112 ms
4,380 KB
testcase_10 AC 112 ms
4,376 KB
testcase_11 AC 115 ms
4,380 KB
testcase_12 AC 117 ms
4,380 KB
testcase_13 AC 124 ms
4,376 KB
testcase_14 AC 114 ms
4,376 KB
testcase_15 AC 127 ms
4,380 KB
testcase_16 AC 152 ms
4,380 KB
testcase_17 AC 137 ms
4,380 KB
testcase_18 AC 117 ms
4,380 KB
testcase_19 AC 119 ms
4,376 KB
testcase_20 AC 154 ms
4,376 KB
testcase_21 AC 149 ms
4,376 KB
testcase_22 AC 140 ms
4,380 KB
testcase_23 AC 158 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template < int n, class... T >
typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple(
    std::ostream &, std::tuple< T... > const &) {}
template < int n, class... T >
typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple(
    std::ostream &os, std::tuple< T... > const &t) {
  os << (n == 0 ? "" : ", ") << std::get< n >(t);
  __output_tuple< n + 1 >(os, t);
}
template < class... T >
std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) {
  os << "(";
  __output_tuple< 0 >(os, t);
  os << ")";
  return os;
}
template < class T, class U >
std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template < class T >
std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) {
  os << "{";
  for(auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template < class T, class Container, class Compare >
std::ostream &operator<<(std::ostream &os,
    std::priority_queue< T, Container, Compare > a) {
  os << "{ (top) ";
  while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template < class T, class Container >
std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) {
  os << "{ ";
  while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
#define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                                \
  [&]() {                                                                        \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                             \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \
    << std::endl;                                                      \
  }()
template < class T >
inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for(size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template < class T >
inline void dump1D(T &d, size_t sizey) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        os << "{";
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : ", ") << *ite;
        os << "}";
        return os;
      }
#else
#define dump(...) ((void) 42)
#define dump2D(...) ((void) 42)
#define dump1D(...) ((void) 42)
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : " ") << *ite;
        return os;
      }
#endif
// }}}


ll a[25];
ll ans = 1e18;
ll sum = 0;

// int dn[] = {-6, -5, -4, +1, +6, +5, +4, -1};
int dn[] = {-5, +1, -1, +5};

// 2e7 * 25 * 定数 ぐらい
// 間に合えパワー~~~w

void dfs(int from, int i, ll use, ll sc) {
  if(i == 0) {
    if(abs(sc) < ans) {
      queue<int> q;
      vector<int> used(25);
      for(int k = 0; k < 25; k++) if(use & (1ll << k)) { q.emplace(k); used[k] = 1; break; }
      for(int k = 0; k < 25; k++) if(not(use & (1ll << k))) { q.emplace(k); used[k] = 1; break; }
      int cnt = 2;
      while(q.size()) {
        int now = q.front();
        q.pop();
        used[now] = 1;
        for(int d = 0; d < 4; d++) {
          int nxt = now + dn[d];
          if((d != 1 or now % 5 != 4) and (d != 2 or now % 5 != 0) and 0 <= nxt and nxt < 25 and used[nxt] == 0 and 
              (
               ((use >> now) & 1) ==
               ((use >> nxt) & 1)
               )
              ) {
            used[nxt] = 1;
            cnt++;
            q.emplace(nxt);
          }
        }
      }
      if(cnt == 25) {
        ans = abs(sc);
      }
    }
    return;
  }
  for(int k = from; k < 25 - i + 1; k++) {
    dfs(k + 1, i - 1, use | (1ll << k), sc - a[k] * 2);
  }
}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) cin >> a[i*5+j], sum += a[i*5+j];

  for(int i = 1; i <= 12; i++) {
    dfs(0, i, 0, sum);
  }

  cout << ans << endl;
  return 0;
}
0