結果

問題 No.2473 Fraises dans une boîte
ユーザー Xiaohuba
提出日時 2025-05-24 21:59:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 245 ms / 4,000 ms
コード長 3,492 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 197,568 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-24 21:59:57
合計ジャッジ時間 8,890 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128
// #undef DEBUG

#ifdef DEBUG
#include <moeebius/debug.hpp>
#else
#define debug(...) (void(0))
#define Debug(...) (void(0))
#endif

#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif

#if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define ssz(x) (signed((x).size()))
#define beg2ed(x) (x).begin(), (x).end()
#define _loop_i_t(j, k) make_signed_t<decltype((j) - (k))>
#define For(i, j, k) for (_loop_i_t(j, k) i = (j); i <= (k); ++i)
#define ForDown(i, j, k) for (_loop_i_t(j, k) i = (j); i >= decltype(i)(k); --i)
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
constexpr il ll qpow(ll x, ull y, ll mod){ ll ans = 1; x %= mod; while (y) { if (y & 1) (ans *= x) %= mod; (x *= x) %= mod; y >>= 1; } return ans; }
template<typename T> constexpr il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> constexpr il void cmax(T & x, const T &y){ x = max(x, y); }
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + (c - '0'); c = getchar(); } x *= f; }
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x), read(y...); }
// clang-format on

// File head end

namespace {
// constexpr ll MAXN = ...;
int n, m, a[605][605], sum[605][605], f[605][605];
int S(int x1, int y1, int x2, int y2) {
  if (x1 > x2 || y1 > y2)
    return 0;
  return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
}
void Main() {
  read(n, m);
  For(i, 1, n) For(j, 1, m) read(a[i][j]), sum[i][j] = a[i][j];
  For(i, 1, n) For(j, 1, m) sum[i][j] += sum[i][j - 1];
  For(i, 1, n) For(j, 1, m) sum[i][j] += sum[i - 1][j];
  memset(f, 0x3f, sizeof f);
  For(i, 1, n) f[i][m] = 0;
  For(i, 1, m) f[n][i] = 0;
  ForDown(i, n - 1, 1) ForDown(j, m - 1, 1) {
    vector<int> R, C;
    For(k, i, n) if (S(k, j, k, m)) R.eb(k);
    For(k, j, m) if (S(i, k, n, k)) C.eb(k);
    if (ssz(R) <= 1 || ssz(C) <= 1) {
      f[i][j] = 0;
      continue;
    }
    f[i][j] = ssz(R) - S(i, C[0], n, C[0]) + f[i][C[1]];
    for (int p = ssz(R) - 1, q = 1; p; p--) {
      while (q < ssz(C) && S(R[p], C[q], n, m))
        q++;
      cmin(f[i][j], f[R[p]][j] + (q < ssz(C) ? f[i][C[q]] : 0) +
                        (p * q - S(i, j, R[p - 1], C[q - 1])));
    }
  }
  cout << f[1][1] << '\n';
}
} // namespace

signed main() { return Main(), 0; }
0