結果

問題 No.2473 Fraises dans une boîte
ユーザー 👑 tute7627tute7627
提出日時 2023-09-08 23:16:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,368 ms / 4,000 ms
コード長 1,677 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 203,668 KB
最終ジャッジ日時 2025-02-16 20:24:22
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

int main(){
  int H, W;
  cin >> H >> W;
  assert(1 <= H && H <= 300);
  assert(1 <= W && W <= 300);
  vector a(H + 1, vector(W + 1, 0));
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      cin >> a[i + 1][j + 1];
      assert(0 <= a[i + 1][j + 1] && a[i + 1][j + 1] <= 1);
    }
  }
  for(int i = 0; i <= H; i++){
    for(int j = 0; j < W; j++){
      a[i][j + 1] += a[i][j];
    }
  }
  for(int i = 0; i < H; i++){
    for(int j = 0; j <= W; j++){
      a[i + 1][j] += a[i][j];
    }
  }
  auto query = [&](int lx, int rx, int ly, int ry){
    return a[rx][ry] - a[lx][ry] - a[rx][ly] + a[lx][ly];
  };
  vector<pair<int, int>>points;
  for(int i = 1; i <= H; i++){
    for(int j = 1; j <= W; j++){
      if(query(i, H, j, W) == 0 && query(i-1, H, j - 1, W) != 0){
        points.emplace_back(i, j);
      }
    }
  }
  vector dp(H + 1, vector(W + 1, 0));
  for(int i = H - 1; i >= 0; i--){
    for(int j = W - 1; j >= 0; j--){
      dp[i][j] = 1e9;
      vector<int>row,col;
      for(int k = i; k < H; k++){
        if(query(k, k+1, j, W) > 0)row.push_back(k);
      }
      for(int k = j; k < W; k++){
        if(query(i, H, k, k+1) > 0)col.push_back(k);
      }
      if(row.empty()){
        dp[i][j] = 0;
        continue;
      }
      for(auto [x, y]: points){
        if(i >= x || j >= y)continue;
        int row_cnt = lower_bound(row.begin(), row.end(), x) - row.begin();
        int col_cnt = lower_bound(col.begin(), col.end(), y) - col.begin();
        dp[i][j] = min(dp[i][j], row_cnt * col_cnt - query(i, x, j, y) + dp[x][j] + dp[i][y]);
      }
    }
  }
  cout << dp[0][0] << endl;
  return 0;
}
0