- 完美矩形
请问为什么会compile time out 呢? Golang
- 2024-7-4 22:14:00 @
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
ns := sc.Text()
n, _ := strconv.Atoi(ns)
g := []string{}
for i := 0; i < n; i++ {
sc.Scan()
g = append(g, sc.Text())
}
// 二维切片初始化
preSum := make([][]int, n+1)
for i := 0; i <= n; i++ {
preSum[i] = make([]int, n+1)
}
for i := 1; i < n+1; i++ {
for j := 1; j < n+1; j++ {
temp := 0
if g[i-1][j-1] == '1' {
temp = 1
}
preSum[i][j] = preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1] + temp
}
}
var query func(int, int, int, int) int
query = func(x1, y1, x2, y2 int) int {
return preSum[x2][y2] - preSum[x2][y1-1] - preSum[x1-1][y2] + preSum[x1-1][y1-1]
}
for i := 1; i < n+1; i++ {
if i%2 == 1 {
fmt.Println(0)
continue
}
ans := 0
for x := 1; x <= n-i+1; x++ {
for y := 1; y <= n-i+1; y++ {
if query(x, y, x+i-1, y+i-1)*2 == i*i {
ans++
}
}
}
fmt.Println(ans)
}
}
0 comments
No comments so far...
Information
- ID
- 6
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 4
- Tags
- # Submissions
- 476
- Accepted
- 36
- Uploaded By