結果

問題 No.866 レベルKの正方形
ユーザー MarcusAureliusAntoninus
提出日時 2019-08-16 23:30:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,532 ms / 6,000 ms
コード長 1,575 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 203,896 KB
最終ジャッジ日時 2025-01-07 13:00:23
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:20: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   38 |         printf("%lld\n", ans);
      |                 ~~~^     ~~~
      |                    |     |
      |                    |     int64_t {aka long int}
      |                    long long int
      |                 %ld
main.cpp:14:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |         scanf("%d%d%d", &H, &W, &K);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |                         scanf(" %c", &c);
      |                         ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using vi26 = std::vector<std::array<int, 26>>;
using vvi26 = std::vector<vi26>;

int H, W, K;
vvi26 table;

int scaleCount(int, int, int);
int calcLevel(int, int, int, int);

int main()
{
	scanf("%d%d%d", &H, &W, &K);
	table.resize(H + 1, vi26(W + 1));
	for (int h{1}; h <= H; h++)
		for (int w{1}; w <= W; w++)
		{
			char c;
			scanf(" %c", &c);
			table[h][w][c - 'a']++;
		}
	for (int h{1}; h <= H; h++)
		for (int w{1}; w <= W; w++)
			for (int c{}; c < 26; c++)
				table[h][w][c] += table[h][w - 1][c];
	for (int h{1}; h <= H; h++)
		for (int w{}; w <= W; w++)
			for (int c{}; c < 26; c++)
				table[h][w][c] += table[h - 1][w][c];
	int64_t ans{};
	for (int h{H - 1}; h > 0; h--)
		ans += scaleCount(h, 0, K) - scaleCount(h, 0, K + 1);
	ans += scaleCount(0, 0, K) - scaleCount(0, 0, K + 1);
	for (int w{W}; w > 0; w--)
		ans += scaleCount(0, w, K) - scaleCount(0, w, K + 1);

	printf("%lld\n", ans);

	return 0;
}

int scaleCount(int begin_h, int begin_w, int K)
{
	int ans{};
	for (int right{1}, left{}; begin_h + right <= H && begin_w + right <= W; right++)
	{
		if (calcLevel(begin_h + left, begin_w + left, begin_h + right, begin_w + right) < K) continue;
		while (calcLevel(begin_h + left + 1, begin_w + left + 1, begin_h + right, begin_w + right) >= K)
			left++;
		ans += left + 1;
	}
	return ans;
}

int calcLevel(int top, int left, int bottom, int right)
{
	int level{};
	for (int c{}; c < 26; c++)
		if (table[bottom][right][c] + table[top][left][c] - table[top][right][c] - table[bottom][left][c] > 0)
			level++;
	return level;
}
0