結果

問題 No.866 レベルKの正方形
ユーザー 👑 emthrmemthrm
提出日時 2019-08-16 23:34:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,750 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 136,184 KB
実行使用メモリ 435,768 KB
最終ジャッジ日時 2023-10-24 06:09:38
合計ジャッジ時間 10,595 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int h, w, K; cin >> h >> w >> K;
  vector<vector<char> > c(h, vector<char>(w)); REP(i, h) REP(j, w) cin >> c[i][j];
  vector<vector<vector<int> > > cnt(26, vector<vector<int> >(h, vector<int>(w, 0)));
  REP(i, h) {
    REP(j, w) {
      REP(k, 26) if (j > 0) cnt[k][i][j] = cnt[k][i][j - 1];
      ++cnt[c[i][j] - 'a'][i][j];
    }
  }
  REP(j, w) {
    FOR(i, 1, h) REP(k, 26) cnt[k][i][j] += cnt[k][i - 1][j];
  }
  int plus;
  for (plus = 0; plus * plus < K; ++plus) {}
  --plus;
  long long ans = 0;
  REP(i, h) REP(j, w) {
    if ((h - i) * (w - j) < K) continue;
    int tmp = (i < j ? h - (j - i) - 1 : h - 1);
    vector<int> memo(tmp - i + 1, -1);
    memo[tmp - i] = 0;
    int Y1 = i, X1 = j, Y2 = tmp, X2 = j + tmp - i;
    REP(k, 26) {
      int res = cnt[k][Y2][X2];
      if (res == 0) continue;
      if (Y1 > 0) res -= cnt[k][Y1 - 1][X2];
      if (X1 > 0) res -= cnt[k][Y2][X1 - 1];
      if (Y1 > 0 && X1 > 0) res += cnt[k][Y1 - 1][X1 - 1];
      if (res > 0) ++memo[tmp - i];
      if (memo[tmp - i] + (25 - k) < K) break;
      if (memo[tmp - i] > K) break;
    }
    // cout << tmp << (j == w - 1 ? '\n' : ' ');
    if (memo[tmp - i] < K) continue;
    int lb = i - 1 + plus, ub = tmp;
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      int l = memo[mid - i];
      if (l == -1) {
        int y1 = i, x1 = j, y2 = mid, x2 = j + mid - i;
        l = 0;
        REP(k, 26) {
          int res = cnt[k][y2][x2];
          if (res == 0) continue;
          if (y1 > 0) res -= cnt[k][y1 - 1][x2];
          if (x1 > 0) res -= cnt[k][y2][x1 - 1];
          if (y1 > 0 && x1 > 0) res += cnt[k][y1 - 1][x1 - 1];
          if (res > 0) ++l;
          if (l + (25 - k) < K) break;
          if (l > K) break;
        }
        memo[mid - i] = l;
      }
      (l >= K ? ub : lb) = mid;
    }
    int mn = ub;
    lb = i - 1 + plus; ub = tmp;
    if (memo[tmp - i] > K) {
      while (ub - lb > 1) {
        int mid = (lb + ub) / 2;
        int l = memo[mid - i];
        if (l == -1) {
          int y1 = i, x1 = j, y2 = mid, x2 = j + mid - i;
          l = 0;
          REP(k, 26) {
            int res = cnt[k][y2][x2];
            if (res == 0) continue;
            if (y1 > 0) res -= cnt[k][y1 - 1][x2];
            if (x1 > 0) res -= cnt[k][y2][x1 - 1];
            if (y1 > 0 && x1 > 0) res += cnt[k][y1 - 1][x1 - 1];
            if (res > 0) ++l;
            if (l + (25 - k) < K) break;
            if (l > K) break;
          }
          memo[mid - i] = l;
        }
        (l > K ? ub : lb) = mid;
      }
    }
    int mx = ub + (memo[tmp - i] <= K ? 1 : 0);
    ans += mx - mn;
    // cout << i << ' ' << j << ' ' << mn << ' ' << mx << '\n';
  }
  cout << ans << '\n';
  return 0;
}
0