結果

問題 No.2473 Fraises dans une boîte
ユーザー 👑 tute7627tute7627
提出日時 2023-09-08 23:16:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,416 ms / 4,000 ms
コード長 1,677 bytes
コンパイル時間 2,204 ms
コンパイル使用メモリ 210,132 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 23:17:03
合計ジャッジ時間 24,537 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 42 ms
4,380 KB
testcase_41 AC 38 ms
4,376 KB
testcase_42 AC 5 ms
4,376 KB
testcase_43 AC 224 ms
4,376 KB
testcase_44 AC 234 ms
4,376 KB
testcase_45 AC 37 ms
4,380 KB
testcase_46 AC 513 ms
4,380 KB
testcase_47 AC 749 ms
4,376 KB
testcase_48 AC 707 ms
4,380 KB
testcase_49 AC 1,334 ms
4,376 KB
testcase_50 AC 226 ms
4,380 KB
testcase_51 AC 1,155 ms
4,376 KB
testcase_52 AC 1,210 ms
4,380 KB
testcase_53 AC 1,377 ms
4,384 KB
testcase_54 AC 721 ms
4,380 KB
testcase_55 AC 1,336 ms
4,380 KB
testcase_56 AC 266 ms
4,380 KB
testcase_57 AC 745 ms
4,376 KB
testcase_58 AC 159 ms
4,380 KB
testcase_59 AC 135 ms
4,376 KB
testcase_60 AC 1,416 ms
4,380 KB
testcase_61 AC 574 ms
4,376 KB
testcase_62 AC 950 ms
4,376 KB
testcase_63 AC 317 ms
4,380 KB
testcase_64 AC 218 ms
4,376 KB
testcase_65 AC 1,349 ms
4,380 KB
testcase_66 AC 176 ms
4,376 KB
testcase_67 AC 65 ms
4,376 KB
testcase_68 AC 1,336 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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