結果

問題 No.866 レベルKの正方形
ユーザー MisterMister
提出日時 2020-08-05 02:18:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,263 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 83,588 KB
実行使用メモリ 431,688 KB
最終ジャッジ日時 2023-10-13 06:16:08
合計ジャッジ時間 10,663 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,700 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 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 <iostream>
#include <algorithm>
#include <vector>
#include <string>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

using lint = long long;

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

    auto acc = vec(26, vec(h + 1, vec(w + 1, 0)));
    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];
            }
        }
    }

    auto get = [&](int lx, int ly, int rx, int ry) {
        static std::vector<int> cnt(26);
        std::fill(cnt.begin(), cnt.end(), 0);

        for (int d = 0; d < 26; ++d) {
            cnt[d] = acc[d][rx][ry] - acc[d][rx][ly - 1] -
                     acc[d][lx - 1][ry] + acc[d][lx - 1][ly - 1];
        }
        return std::count_if(cnt.begin(), cnt.end(),
                             [](auto c) { return c > 0; });
    };

    lint ans = 0;
    for (int lx = 1; lx <= h; ++lx) {
        for (int ly = 1; ly <= w; ++ly) {
            int dl, dr;
            {
                int ok = std::min(h - lx, w - ly) + 1, ng = -1;
                // [lx, lx + ok] * [ly, ly + ok] >= k
                while (ok - ng > 1) {
                    int mid = (ok + ng) / 2;
                    if (get(lx, ly, lx + mid, ly + mid) >= k) {
                        ok = mid;
                    } else {
                        ng = mid;
                    }
                }
                dl = ok;
            }

            {
                int ok = std::min(h - lx, w - ly) + 1, ng = -1;
                // [lx, lx + ok] * [ly, ly + ok] > k
                while (ok - ng > 1) {
                    int mid = (ok + ng) / 2;
                    if (get(lx, ly, lx + mid, ly + mid) > 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