結果

問題 No.866 レベルKの正方形
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-10 00:03:27
言語 Go
(1.22.1)
結果
AC  
実行時間 2,018 ms / 6,000 ms
コード長 2,828 bytes
コンパイル時間 14,003 ms
コンパイル使用メモリ 236,656 KB
実行使用メモリ 257,540 KB
最終ジャッジ日時 2024-06-11 04:58:54
合計ジャッジ時間 38,133 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 1 ms
6,812 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1,471 ms
257,496 KB
testcase_09 AC 1,562 ms
257,536 KB
testcase_10 AC 1,913 ms
257,540 KB
testcase_11 AC 1,948 ms
257,532 KB
testcase_12 AC 1,937 ms
257,536 KB
testcase_13 AC 1,912 ms
257,540 KB
testcase_14 AC 1,887 ms
257,532 KB
testcase_15 AC 1,838 ms
257,536 KB
testcase_16 AC 2,018 ms
257,540 KB
testcase_17 AC 1,942 ms
257,536 KB
testcase_18 AC 1,909 ms
257,536 KB
testcase_19 AC 1,825 ms
257,540 KB
testcase_20 AC 1,559 ms
257,540 KB
testcase_21 AC 1,695 ms
257,540 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"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)
	}
	sq := makeGrid(h, w)
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			sq[y][x].b[0] = sq[y][x].b[0] | uint32(1<<uint(ss[y][x]-'a'))
		}
	}
	for i := 1; i < 16; i++ {
		for y := 0; y < h && y+1<<uint(i) <= h; y++ {
			for x := 0; x < w && x+1<<uint(i) <= w; x++ {
				sq[y][x].b[i] = sq[y][x].b[i] | sq[y][x].b[i-1]
				sq[y][x].b[i] = sq[y][x].b[i] | sq[y+1<<uint(i-1)][x].b[i-1]
				sq[y][x].b[i] = sq[y][x].b[i] | sq[y][x+1<<uint(i-1)].b[i-1]
				sq[y][x].b[i] = sq[y][x].b[i] | sq[y+1<<uint(i-1)][x+1<<uint(i-1)].b[i-1]
			}
		}
	}
	lowers := make([]int, 2)
	var ans int64
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			for i := 0; i < 2; i++ {
				l := 0
				r := min(h-y, w-x) + 1
				for l+1 < r {
					m := (l + r) >> 1
					z := 32 - bits.LeadingZeros32(uint32(m))
					b := sq[y][x].b[z-1]
					b = b | sq[y+m-1<<uint(z-1)][x].b[z-1]
					b = b | sq[y][x+m-1<<uint(z-1)].b[z-1]
					b = b | sq[y+m-1<<uint(z-1)][x+m-1<<uint(z-1)].b[z-1]
					if bits.OnesCount32(b) < k+i {
						l = m
						continue
					}
					r = m
				}
				lowers[i] = r
			}
			ans += int64(lowers[1] - lowers[0])
		}
	}
	fmt.Fprintln(writer, ans)
}

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

type square struct {
	b [16]uint32
}

func makeGrid(h, w int) [][]square {
	index := make([][]square, h, h)
	data := make([]square, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
0