結果

問題 No.2456 Stamp Art
ユーザー tnakao0123
提出日時 2024-06-22 02:05:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,448 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 63,736 KB
最終ジャッジ日時 2025-02-22 00:05:52
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 TLE * 2 -- * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:123:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  123 |   scanf("%d%d", &h, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:124:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  124 |   for (int y = 0; y < h; y++) scanf("%s", fs[y]);
      |                               ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2456.cc:  No.2456 Stamp Art - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_H = 2000;
const int MAX_W = 2000;

/* typedef */

template <typename T>
struct BIT {
  int n;
  vector<T> bits;
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    bits.assign(n + 1, 0);
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

template <typename T>
struct BIT2D {
  int n;
  vector<BIT<T>> bits;
  
  BIT2D() {}
  BIT2D(int _ny, int _nx) { init(_ny, _nx); }

  void init(int _ny, int _nx) {
    n = _ny;
    bits.assign(n + 1, BIT<T>());
    for (int y = 1; y <= n; y++) bits[y].init(_nx);
  }

  T sum(int y, int x) {
    y = min(y, n);
    T s = 0;
    while (y > 0) {
      s += bits[y].sum(x);
      y -= (y & -y);
    }
    return s;
  }

  void add(int y, int x, T v) {
    if (y <= 0) return;
    while (y <= n) {
      bits[y].add(x, v);
      y += (y & -y);
    }
  }
};

/* global variables */

char fs[MAX_H][MAX_W + 4];
int ass[MAX_H + 1][MAX_W + 1];

/* subroutines */

inline int rect(int y0, int x0, int y1, int x1) {
  return
    ass[y1][x1] - ass[y1][x0] - ass[y0][x1] + ass[y0][x0];
}

bool check(int h, int w, int k) {
  BIT2D<int> bit;
  bit.init(h, w);
  int kk = k * k;

  for (int y = 0; y + k <= h; y++)
    for (int x = 0; x + k <= w; x++)
      if (rect(y, x, y + k, x + k) == kk) {
	bit.add(y + 1, x + 1, 1);
	bit.add(y + k + 1, x + 1, -1);
	bit.add(y + 1, x + k + 1, -1);
	bit.add(y + k + 1, x + k + 1, 1);
      }

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
      int c = bit.sum(y + 1, x + 1);
      if ((fs[y][x] == '#' && c == 0) ||
	  (fs[y][x] == '.' && c > 0)) return false;
    }
  return true;
}

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int y = 0; y < h; y++) scanf("%s", fs[y]);

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      ass[y + 1][x + 1] =
	(fs[y][x] == '#' ? 1 : 0)
	+ ass[y + 1][x] + ass[y][x + 1] - ass[y][x];
  
  int k0 = 1, k1 = min(h, w) + 1;
  while (k0 + 1 < k1) {
    int k = (k0 + k1) / 2;
    if (check(h, w, k)) k0 = k;
    else k1 = k;
  }

  printf("%d\n", k0);
  
  return 0;
}
0