結果

問題 No.866 レベルKの正方形
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-09 22:55:09
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 2,753 bytes
コンパイル時間 11,311 ms
コンパイル使用メモリ 209,352 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-31 01:16:32
合計ジャッジ時間 22,181 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	h := getNextInt(scanner)
	w := getNextInt(scanner)
	k := getNextInt(scanner)
	ss := make([]string, h)
	for i := 0; i < h; i++ {
		ss[i] = getNextString(scanner)
	}
	imim := make([]imos, 26)
	for i := 0; i < 26; i++ {
		imim[i] = newImos(h, w, byte('a'+i), ss)
	}
	var ans int
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			lowers := make([]int, 2)
			for lw := 0; lw < 2; lw++ {
				l := 0
				r := min(h-y, w-x) + 1
				for 1 < r-l {
					m := (l + r) >> 1
					cnt := 0
					for i := 0; i < 26; i++ {
						if imim[i].count(y, x, y+m, x+m) > 0 {
							cnt++
						}
					}
					if cnt < k+lw {
						l = m
						continue
					}
					r = m
				}
				lowers[lw] = r
			}
			ans += lowers[1] - lowers[0]
		}
	}
	fmt.Fprintln(writer, ans)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type imos struct {
	c [][]int32
}

func newImos(h, w int, ch byte, ss []string) imos {
	c := makeGrid(h+1, w+1)
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			if ss[y][x] == ch {
				c[y+1][x+1] = 1
			}
		}
	}
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			c[y+1][x+1] += c[y+1][x]
		}
	}
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			c[y+1][x+1] += c[y][x+1]
		}
	}
	return imos{
		c: c,
	}
}

func (im *imos) count(fy, fx, ty, tx int) int32 {
	return im.c[ty][tx] - im.c[ty][fx] - im.c[fy][tx] + im.c[fy][fx]
}
func makeGrid(h, w int) [][]int32 {
	index := make([][]int32, h, h)
	data := make([]int32, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
0