結果

問題 No.2473 Fraises dans une boîte
ユーザー Enucai
提出日時 2025-05-14 00:17:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 152 ms / 4,000 ms
コード長 1,410 bytes
コンパイル時間 2,116 ms
コンパイル使用メモリ 196,204 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-14 00:17:50
合計ジャッジ時間 7,693 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

const int maxn = 310;

int n, m, a[maxn][maxn], f[maxn][maxn], cnt[maxn][maxn], px[maxn], py[maxn];

int calc(int xl, int xr, int yl, int yr) {
  return cnt[xr][yr] - cnt[xl - 1][yr] - cnt[xr][yl - 1] + cnt[xl - 1][yl - 1];
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> m;
  rep (i, 1, n) rep (j, 1, m) cin >> a[i][j], cnt[i][j] = a[i][j];
  rep (i, 1, n) rep (j, 1, m) cnt[i][j] += cnt[i - 1][j];
  rep (i, 1, n) rep (j, 1, m) cnt[i][j] += cnt[i][j - 1];
  per (i, n, 1) per (j, m, 1) {
    px[i - 1] = py[j - 1] = 0;
    rep (k, i, n) px[k] = px[k - 1] + (calc(k, k, j, m) > 0);
    rep (k, j, m) py[k] = py[k - 1] + (calc(i, n, k, k) > 0);
    f[i][j] = px[n] * py[m] - calc(i, n, j, m);
    for (int x = i, y = m; x <= n; x++) {
      while (y > j && !calc(x + 1, n, y, m)) y--;
      chkmn(f[i][j], f[x + 1][j] + f[i][y + 1] + px[x] * py[y] - calc(i, x, j, y));
    }
  }
  cout << f[1][1] << '\n';
}
0