結果

問題 No.861 ケーキカット
ユーザー Iván SotoIván Soto
提出日時 2021-05-28 08:33:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 643 ms / 1,000 ms
コード長 3,332 bytes
コンパイル時間 2,487 ms
コンパイル使用メモリ 206,400 KB
実行使用メモリ 134,400 KB
最終ジャッジ日時 2024-04-24 15:32:29
合計ジャッジ時間 19,026 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 620 ms
134,272 KB
testcase_01 AC 614 ms
134,272 KB
testcase_02 AC 640 ms
134,400 KB
testcase_03 AC 633 ms
134,272 KB
testcase_04 AC 630 ms
134,272 KB
testcase_05 AC 626 ms
134,272 KB
testcase_06 AC 611 ms
134,272 KB
testcase_07 AC 611 ms
134,400 KB
testcase_08 AC 616 ms
134,272 KB
testcase_09 AC 603 ms
134,272 KB
testcase_10 AC 616 ms
134,272 KB
testcase_11 AC 604 ms
134,400 KB
testcase_12 AC 630 ms
134,272 KB
testcase_13 AC 608 ms
134,400 KB
testcase_14 AC 599 ms
134,272 KB
testcase_15 AC 620 ms
134,272 KB
testcase_16 AC 626 ms
134,144 KB
testcase_17 AC 621 ms
134,272 KB
testcase_18 AC 639 ms
134,272 KB
testcase_19 AC 611 ms
134,272 KB
testcase_20 AC 623 ms
134,400 KB
testcase_21 AC 641 ms
134,272 KB
testcase_22 AC 621 ms
134,400 KB
testcase_23 AC 643 ms
134,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Thu May 27 2021
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
template<typename T> void dout(string name, int idx, T arg) {
  cerr << name << " = " << to_string(arg);
}

template<typename T1, typename... T2> void dout(string names, int idx, T1 arg, T2... args) {
  cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", ";
  dout(names.substr(names.find(',') + 2), idx + 1, args...);
}

#ifdef LOCAL
#define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl;
#else
#define debug(...) 42
#endif

class dsu {
  public:
  vector<int> p;
  int n;
  int com;
  dsu(int _n) : n(_n) {
    p.resize(n);
    com = n;
    iota(p.begin(), p.end(), 0);
  }
  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }
  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      --com;
      return true;
    }
    return false;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  const int n = 5;
  vector<vector<int>> c(n, vector<int>(n));
  long long total = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> c[i][j];
      total += c[i][j];
    }
  }
  long long ans = (long long) 1e18;
  const vector<int> dx = {-1, 0, 1, 0};
  const vector<int> dy = {0, -1, 0, 1};
  vector<int> was(1 << (n * n));
  function<bool(int, int, int)> On = [&](int mask, int x, int y) {
    return (mask >> (x * n + y)) & 1;
  };
  function<void(int, long long)> DFS = [&](int mask, long long sum) {
    was[mask] = 1;
    long long res = abs(sum - (total - sum));
    if (res < ans) {
      dsu d(n * n);
      for (int x = 0; x < n; x++) {
        for (int y = 0; y < n; y++) {
          if (x - 1 >= 0 && On(mask, x, y) == On(mask, x - 1, y)) {
            d.unite(x * n + y, (x - 1) * n + y);
          }
          if (y - 1 >= 0 && On(mask, x, y) == On(mask, x, y - 1)) {
            d.unite(x * n + y, x * n + y - 1);
          }
        }
      }
      if (d.com <= 2) {
        ans = res;
      }
    }
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < n; y++) {
        if (On(mask, x, y)) {
          continue;
        }
        for (int k = 0; k < 4; k++) {
          int xk = x + dx[k];
          int yk = y + dy[k];
          if (0 <= xk && xk < n && 0 <= yk && yk < n && On(mask, xk, yk) && !was[mask | (1 << (x * n + y))]) {
            DFS(mask | (1 << (x * n + y)), sum + c[x][y]);
          }
        }
      }
    }
  };
  DFS(1, c[0][0]);
  cout << ans << '\n';
  return 0;
}
0