結果

問題 No.866 レベルKの正方形
ユーザー onakaT_TitaionakaT_Titai
提出日時 2019-08-17 00:08:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,934 ms / 6,000 ms
コード長 2,965 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 114,204 KB
実行使用メモリ 414,156 KB
最終ジャッジ日時 2023-10-24 12:33:14
合計ジャッジ時間 27,632 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,716 KB
testcase_01 AC 3 ms
7,716 KB
testcase_02 AC 3 ms
7,716 KB
testcase_03 AC 3 ms
7,716 KB
testcase_04 AC 2 ms
7,716 KB
testcase_05 AC 2 ms
7,716 KB
testcase_06 AC 2 ms
7,716 KB
testcase_07 AC 2 ms
7,716 KB
testcase_08 AC 1,871 ms
414,080 KB
testcase_09 AC 1,743 ms
414,156 KB
testcase_10 AC 1,934 ms
414,156 KB
testcase_11 AC 1,926 ms
414,156 KB
testcase_12 AC 1,532 ms
414,156 KB
testcase_13 AC 1,887 ms
414,156 KB
testcase_14 AC 1,543 ms
414,156 KB
testcase_15 AC 1,914 ms
414,156 KB
testcase_16 AC 1,800 ms
414,156 KB
testcase_17 AC 1,933 ms
414,156 KB
testcase_18 AC 1,904 ms
414,156 KB
testcase_19 AC 1,380 ms
414,156 KB
testcase_20 AC 1,374 ms
414,156 KB
testcase_21 AC 1,521 ms
414,156 KB
testcase_22 AC 2 ms
5,648 KB
testcase_23 AC 2 ms
5,648 KB
testcase_24 AC 3 ms
9,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
#include <cassert>
#include <random>
#include <time.h>
//#include <boost/multiprecision/cpp_int.hpp>



using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const double EPS = 1e-10;
long long const MOD = 1000000007;

long long mod_pow(long long x, long long n) {
    long long res = 1;
    for (int i = 0;i < 60; i++) {
        if (n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

void fastInput() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

vector<long long> Divisor(long long n) {
    vector<long long> ret;
    for(long long i=1; i*i<=n; i++) {
        if(n % i == 0) {
            ret.push_back(i);
            if(i*i!=n) ret.push_back(n / i);
        }
    }
    return ret;
}

int ruiseki[2001][2001][26];
char field[2001][2001];
int H, W, K;

int calc(int lw, int lh, int rw, int rh) {
    int ret = 0;
    for (int i = 0; i < 26; i++) {
        int a = ruiseki[rh][rw][i] + ruiseki[lh-1][lw-1][i];
        a -= ruiseki[lh-1][rw][i];
        a -= ruiseki[rh][lw-1][i];
        if (a > 0) ret++;
    }
    return ret;
}

int shakutori(int sW, int sH, int N) {
    int lw = sW;
    int lh = sH;
    int rw = sW;
    int rh = sH;
    long long ret = 0;
    for (; lw <= W && lh <= H; lw++, lh++) {
        while (rw <= W && rh <= H && calc(lw, lh, rw, rh) <= N) {
            rw++;
            rh++;
        }
        ret += rw - lw;
        if (rw < lw) {
            rw++;
            rh++;
        }
    }
    return ret;
}

int main(void) {
    cin >> H >> W >> K;

    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            cin >> field[i][j];
        }
    }

    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            ruiseki[i][j][field[i][j] - 'a']++;
            for (int k = 0; k < 26; k++) {
                ruiseki[i][j][k] += ruiseki[i][j-1][k];
            }
        }
    }

    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            for (int k = 0; k < 26; k++) {
                ruiseki[i][j][k] += ruiseki[i-1][j][k];
            }
        }
    }
    long long ans = 0;
    for (int i = 1; i <= W; i++) {
        ans += shakutori(i, 1, K) - shakutori(i, 1, K-1);
    }
    for (int i = 2; i <= H; i++) {
        ans += shakutori(1, i, K) - shakutori(1, i, K-1);
    }
    cout << ans << endl;
    
}
0