結果

問題 No.866 レベルKの正方形
ユーザー MisterMister
提出日時 2020-08-05 02:30:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,183 bytes
コンパイル時間 563 ms
コンパイル使用メモリ 69,428 KB
実行使用メモリ 414,076 KB
最終ジャッジ日時 2023-10-13 06:34:00
合計ジャッジ時間 9,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,052 KB
testcase_01 AC 3 ms
6,468 KB
testcase_02 AC 3 ms
6,396 KB
testcase_03 AC 4 ms
6,412 KB
testcase_04 AC 4 ms
6,400 KB
testcase_05 AC 3 ms
6,412 KB
testcase_06 AC 3 ms
6,420 KB
testcase_07 AC 4 ms
6,468 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 <iostream>

using lint = long long;

constexpr int N = 2000;

int acc[26][N + 1][N + 1];

void solve() {
    int h, w, k;
    std::cin >> h >> w >> k;

    for (int i = 1; i <= h; ++i) {
        for (int j = 1; j <= w; ++j) {
            char c;
            std::cin >> c;
            ++acc[c - 'a'][i][j];

            for (auto& vv : acc) {
                vv[i][j] += vv[i - 1][j] + vv[i][j - 1] - vv[i - 1][j - 1];
            }
        }
    }

    lint ans = 0;
    int ok, ng, mid, dl, dr, rx, ry, cnt, c;

    for (int lx = 1; lx <= h; ++lx) {
        for (int ly = 1; ly <= w; ++ly) {
            ok = std::min(h - lx, w - ly) + 1, ng = -1;
            // [lx, lx + ok] * [ly, ly + ok] >= k
            while (ok - ng > 1) {
                mid = (ok + ng) / 2;
                rx = lx + mid, ry = ly + mid;

                cnt = 0;
                for (int d = 0; d < 26; ++d) {
                    c = acc[d][rx][ry] - acc[d][rx][ly - 1] -
                        acc[d][lx - 1][ry] + acc[d][lx - 1][ly - 1];
                    if (c > 0) ++cnt;
                    if (cnt >= k) break;
                }

                if (cnt >= k) {
                    ok = mid;
                } else {
                    ng = mid;
                }
            }
            dl = ok;

            ok = std::min(h - lx, w - ly) + 1, ng = dl - 1;
            // [lx, lx + ok] * [ly, ly + ok] > k
            while (ok - ng > 1) {
                mid = (ok + ng) / 2;
                rx = lx + mid, ry = ly + mid;

                cnt = 0;
                for (int d = 0; d < 26; ++d) {
                    c = acc[d][rx][ry] - acc[d][rx][ly - 1] -
                        acc[d][lx - 1][ry] + acc[d][lx - 1][ly - 1];
                    if (c > 0) ++cnt;
                    if (cnt > k) break;
                }

                if (cnt > k) {
                    ok = mid;
                } else {
                    ng = mid;
                }
            }
            dr = ok;

            ans += dr - dl;
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0